简体   繁体   English

在SQL Server 2008中将Varchar HH:MM转换为整数分钟的查询

[英]Query to Convert Varchar HH:MM to Integer Minutes in SQL Server 2008

I am having a table in SQL Server where there is a column name TimeSpent Datatype Varchar(25) . 我在SQL Server中有一个表,其中有一个列名TimeSpent Datatype Varchar(25) It basically stores Time as HH:MM Format. 它基本上将时间存储为HH:MM格式。 As per the requirement now i want that it gives me actual timespent in Minutes ie 01:00 gives me 60, 01:30 -> 90. Please help me to write the query in SQL Server 2008 , so that will convert Varchar (HH:MM) into Minutes(integer). 根据现在的要求,我希望它给我以分钟为单位的实际时间,即01:00给我60、01:30->90。请帮助我在SQL Server 2008中编写查询,以便将Varchar(HH: MM)转换成Minutes(整数)。

Try 尝试

SELECT LTRIM(DATEDIFF(MINUTE, 0, TimeSpent)) 
  FROM yourtable

Given 给定

| TIMESPENT |
-------------
|     00:12 |
|     01:05 |
|     10:00 |

Output: 输出:

| MINUTES |
-----------
|      12 |
|      65 |
|     600 |

Here is SQLFiddle 这是SQLFiddle

This will solve your issue. 这样可以解决您的问题。 Please refer to SQLFiddle 请参考SQLFiddle


select cast(left(TimeSpent, charindex(':',TimeSpent,0)-1) as int)*60+
       cast(right(TimeSpent, len(TimeSpent)-charindex(':',TimeSpent, 0)) as int)
from test

SELECT top 1(substring(CONVERT(VARCHAR(5),TimeSpent,108),1,2)* 60 + substring(CONVERT(VARCHAR(5),TimeSpent,108),4,2)作为测试分钟

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM