我希望调整现有代码,以解决两个datetime
值之间经过的分钟和秒的问题。我目前正在抽出过去的时间,但最好是采用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;
更具体地说,此部分来自上面的代码:
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
datetime
列最初采用以下格式:
2017-05-25T15:58:26.000+0000
当前结果如下:
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
答案 0 :(得分:1)
您可以将秒数转换为时间。我不容易遵循您代码的逻辑,但是转换非常简单:
select cast(dateadd(second,
datediff(second, <starttime>, <enddatime>),
0) as time
)