我想减去两个日期时间值,并以HH:MM:SS格式输出。
如果差异超过一天,则需要将天数加到小时中。
我试图如下所述连接datediff输出
select concat(datediff(hh, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000')
,':',
(datediff(mi, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000') -(datediff(hh, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000')*60))
,':',
(datediff(ss, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000')-(datediff(mi, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000')*60)))
预期输出:38:53:10
实际输出:39:-7:10
你能告诉我我犯了什么错误吗?有没有更好的方法来获得所需的输出?
答案 0 :(得分:1)
类似的东西:
with q as
(
select datediff(SECOND, '2019-07-16 04:45:03.000','2019-07-17 19:38:13.000') secs
), hms as
(
select secs/60/60 as hours, (secs/60) % 60 as minutes , secs % 60 seconds
from q
)
select concat(format(hours,'00'),':',format(minutes,'00'),':',format(seconds,'00') ) hms
from hms
返回
38:53:10