[英]How to compare two datetime to find time difference in SQL Server
I have two columns 我有两栏
Column A: Column B:
2017-08-24 14:01:00.000 2017-08-26 10:12:21.760.
How would it be possible using T-SQL
to find the difference between two dates in hours and minutes? 使用
T-SQL
如何找到小时和分钟两个日期之间的差异?
You can construct the results yourself: 您可以自己构造结果:
select cast(s/3600 as varchar(255)) + ':' + right('00' + cast((s % 3600)/60 as char(2))) as hh:mm
from t cross apply
(values datediff(second, cola, colb)) v(s);
The advantage of this approach over using time
is that it can represent hours greater than 23. 这种方法相对于使用
time
的优势在于,它可以表示大于23的小时。
Use datediff()
function : 使用
datediff()
函数:
select dateadd(ss, datediff(ss, cola, colb), 0) as diff
from table t;
this is another option, it would give you a decimal as result, not sure what datatype you are looking for as result 这是另一种选择,它会为您提供一个十进制的结果,不确定您要寻找的数据类型是什么
Declare @dt1 DATETIME = '2017-08-25 00:00:00.000'
Declare @dt2 DATETIME = '2017-08-26 10:12:21.760'
SELECT DATEDIFF(SECOND,@dt1,@dt2)/3600.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.