SQL计算两个时间段相隔时间

时间:2023-01-31 17:15:48

SQL语句:

select cast(floor(datediff(minute,时间1,时间2) / 1440) as varchar)+'天'+

       cast(floor((datediff(minute,时间1,时间2) % 1440)/60) as varchar)+'小时'+

       cast(((datediff(minute,时间1,时间2))-

           (floor(datediff(minute,时间1,时间2) / 1440) *1440)-

           (floor((datediff(minute,时间1,时间2) % 1440)/60)*60)) as varchar)+'分'

 from 表名

返回:xx天xx小时xx分

----------------------------------------------------------------------------------------------------------------

实例:

select oc.ID,oc.CarNumber,oc.PrintTime1,oc.PrintTime2, (

 cast(floor(datediff(minute,oc.PrintTime1,oc.PrintTime2) / 1440) as varchar)+'天'+

       cast(floor((datediff(minute,oc.PrintTime1,oc.PrintTime2) % 1440)/60) as varchar)+'小时'+

       cast(((datediff(minute,oc.PrintTime1,oc.PrintTime2))-

           (floor(datediff(minute,oc.PrintTime1,oc.PrintTime2) / 1440) *1440)-

           (floor((datediff(minute,oc.PrintTime1,oc.PrintTime2) % 1440)/60)*60)) as varchar)+'分') interval
 from t_ordercar oc
left join t_order o on oc.OrderID = o.ID
where o.SupplierName='金冠电厂2'  and  datediff(d,o.AddTime,'2017-12-19') =0

返回结果:

SQL计算两个时间段相隔时间