简体   繁体   English

日期之间的时差(小时)

[英]difference between dates in hours

I have searched many of the threads and cant find the answer I need. 我搜索了许多线程,找不到所需的答案。 I have two datetime columns: 我有两个日期时间列:

BOOKDATIME                BOOKDATIME_OFF 
2013-06-01 12:14:00.000   2013-06-03 07:09:00.000  

What I want to do is to just show the total hour and minute difference between the two dates. 我想做的只是显示两个日期之间的总时差。

When I use: 当我使用时:

CONVERT(TIME, BOOKDATIME_OFF - BOOKDATIME) 
                  AS HourMinuteSecond

I get the error message: Cannot call methods on time 我收到错误消息:无法按时调用方法

DATEDIFF(HH,BOOKDATIME, BOOKDATIME_OFF)

You can use DATEDIFF() and division/modulus division to get difference in total Hours/Minutes: 您可以使用DATEDIFF()和除法/模数除法来获得总小时数/分钟数的差异:

SELECT Hours = DATEDIFF(Minute,'2013-06-01 12:14:00.000','2013-06-03 07:09:00.000')/60
      ,Minutes = DATEDIFF(Minute,'2013-06-01 12:14:00.000','2013-06-03 07:09:00.000')%60

If the DATETIME fields are always less than a day apart then you can also subtract the datetime's and cast as time: 如果DATETIME字段始终相隔不到一天,那么您还可以减去datetime并将其转换为时间:

DECLARE @date DATETIME = GETDATE()
       ,@date2 DATETIME = DATEADD(MINUTE,25,GETDATE())
SELECT CAST(@date2 - @date AS TIME)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM