6

我在 SQL Server 中有一个表,其中有一个列名 TimeSpent Datatype Varchar(25)。它基本上将时间存储为HH:MM格式。根据现在的要求,我希望它给我以分钟为单位的实际时间,即 01:00 给我 60、01:30 -> 90。请帮助我在 SQL Server 2008 中编写查询,以便转换 Varchar (HH: MM) 转换为分钟(整数)。

4

3 回答 3

15

尝试

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

给定

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

输出:

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

这是SQLFiddle

于 2013-03-21T04:58:33.090 回答
2

这将解决您的问题。请参考SQLFiddle


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

于 2013-03-21T06:00:43.417 回答
0

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

于 2019-07-08T08:30:26.943 回答