[英]SQL - datediff returns hours, but I want to add minutes and seconds in HH:MM:SS
I am looking to adapt my existing code to account for minutes and seconds elapsed between two datetime
values. 我希望调整现有代码,以解决两个
datetime
值之间经过的分钟和秒的问题。 I currently pull out the hours passed, but it would be best if instead of just hours, it was in a HH:MM:SS format but I can't seem to find any documents that solve my problem. 我目前正在抽出过去的时间,但最好是采用HH:MM:SS格式,而不是仅仅几个小时,但是我似乎找不到任何能解决我问题的文档。
Select
convert(datetime, Left(Replace(Create_Date, 'T', ' '), 19)) as CreateDate2,
case
when Resolution_date = 'None' then '1999-01-01 12:00:00'
when Resolution_date <> 'None' then convert(datetime, left(replace(resolution_date, 'T', ' '), 19))
end as ResolutionDate2,
datediff(hour, convert(datetime, left(replace(Create_Date, 'T', ' '), 19)),
case
when Resolution_date = 'None' then '1999-01-01 12:00:00'
when Resolution_date <> 'None' then convert(datetime, left(replace(Resolution_date, 'T', ' '), 19))
end) as HoursPassed,
*
from
xyz
where
abc;
More specifically, this section from the above code: 更具体地说,此部分来自上面的代码:
datediff(hour, convert(datetime,Left(Replace(Create_Date, 'T', ' '), 19)),
case
when Resolution_date = 'None' then '1999-01-01 12:00:00'
when Resolution_date <> 'None' then convert(datetime, left(replace(Resolution_date, 'T', ' '), 19))
end) as HoursPassed
The datetime
column initially comes in this format: datetime
列最初采用以下格式:
2017-05-25T15:58:26.000+0000
Results currently look like this: 当前结果如下:
2017-05-23 19:51:59.000 2017-05-24 12:38:17.000 17
2017-05-24 15:10:50.000 2017-05-24 16:31:23.000 1
2017-05-25 15:58:26.000 2017-05-25 16:03:30.000 1
You can convert the number of seconds as a time. 您可以将秒数转换为时间。 I don't easily follow the logic of your code, but the conversion is simple enough:
我不太容易遵循您代码的逻辑,但是转换非常简单:
select cast(dateadd(second,
datediff(second, <starttime>, <enddatime>),
0) as time
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.