![](/img/trans.png)
[英]How to compare two dates to find time difference in SQL Server 2005, date manipulation
[英]How to compare two datetime to find time difference in SQL Server
我有兩欄
Column A: Column B:
2017-08-24 14:01:00.000 2017-08-26 10:12:21.760.
使用T-SQL
如何找到小時和分鍾兩個日期之間的差異?
您可以自己構造結果:
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);
這種方法相對於使用time
的優勢在於,它可以表示大於23的小時。
使用datediff()
函數:
select dateadd(ss, datediff(ss, cola, colb), 0) as diff
from table t;
這是另一種選擇,它會為您提供一個十進制的結果,不確定您要尋找的數據類型是什么
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.