我有以下查询。
select cast(dateadd(minute, datediff(minute, TimeIn, TimeOut), 0) as time(0) )
我从hrs-min-seconds格式的两列中获得结果。
我想以min的格式。因此02:47:00
会读取167
。
答案 0 :(得分:2)
对于SQL Server(也适用于2005年):
select Datediff(mi,convert(datetime,'00:00:00',108), convert(datetime,'02:47:00',108))
答案 1 :(得分:2)
SQL Server查询:
SELECT cast(substring('02:47:00',1,2) AS int)*60+
cast(substring('02:47:00',4,2) AS int)+
cast(substring('02:47:00',7,2) AS int)/60.0 AS minutes
MYSQL查询:
SELECT TIME_TO_SEC('02:47:00') / 60
结果:
| MINUTES |
-----------
| 167 |
答案 2 :(得分:2)
宣布@Time DATETIME = '01:05:00'
选择((DATEPART(HOUR,@ Time)* 60)+(DATEPART(MINUTE,@ Time)))
答案 3 :(得分:1)
试试这个:
datediff(minute, 0, '02:47')
答案 4 :(得分:1)
扩大贾斯汀的答案。这允许小时数大于2位的情况。
${HOME}/libs/rxjava-1.3.1-SNAPSHOT
结果:
declare @time varchar(50) = '102:47:05'
SELECT cast(right(@time,2) AS int)+
cast(left(right(@time,5),2) AS int)*60+
cast(left(@time,len(@time)-6) AS int)*3600 AS seconds,
(cast(right(@time,2) AS int)+
cast(left(right(@time,5),2) AS int)*60+
cast(left(@time,len(@time)-6) AS int)*3600)/60.0 AS minutes
答案 5 :(得分:0)
SELECT DATEDIFF(minute,CAST('00:00' AS TIME), CAST('02:47' AS TIME)) AS difference
给你:
| DIFFERENCE |
--------------
| 167 |
答案 6 :(得分:0)
$time = '02:47:00';
$time = explode(":",$time);
$total = ($a[0]*60)+$a[1];
echo 'Minutes : '.$total;<br>
echo 'Seconds : '.$a[2];
答案 7 :(得分:0)
不幸的是,如果您要对超过24小时的值使用DATEPART函数,则会收到错误消息:
从字符串转换日期和/或时间时转换失败。”
您可以使用以下代码对其进行测试:
declare @Time DATETIME = '32:00:00'
select ((DATEPART(HOUR, @Time)*60) + (DATEPART(MINUTE, @Time)))
为解决此问题,我使用了另一种方法:
declare @tbl table(WorkHrs VARCHAR(8))
insert into @tbl(WorkHrs) values ('02:47:00')
insert into @tbl(WorkHrs) values ('32:00:00')
-- Sum in minutes
SELECT TRY_CAST(([HOURS] * 60) + [MINUTES] + ([SECOND] / 60) AS INT) as TotalInMinutes
FROM (
SELECT
-- Use this aproach to get separated values
SUBSTRING(WorkHrs,1,CHARINDEX(':',WorkHrs)-1) AS [HOURS],
SUBSTRING(WorkHrs,4,CHARINDEX(':',WorkHrs)-1) AS [MINUTES],
SUBSTRING(WorkHrs,7,CHARINDEX(':',WorkHrs)-1) AS [SECOND] -- probably you can ignore this one
FROM @tbl
)
tbl
-- Sum in seconds
SELECT TRY_CAST(([HOURS] * 3600) + ([MINUTES] * 60) + [SECOND] AS INT) as TotalInSeconds
FROM (
SELECT
-- Use this aproach to get separated values
SUBSTRING(WorkHrs,1,CHARINDEX(':',WorkHrs)-1) AS [HOURS],
SUBSTRING(WorkHrs,4,CHARINDEX(':',WorkHrs)-1) AS [MINUTES],
SUBSTRING(WorkHrs,7,CHARINDEX(':',WorkHrs)-1) AS [SECOND]
FROM @tbl
)
tbl
此代码将返回如下: