1

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

4

2 回答 2

1

您可以使用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

如果DATETIME字段总是相隔不到一天,那么您还可以减去日期时间并转换为时间:

DECLARE @date DATETIME = GETDATE()
       ,@date2 DATETIME = DATEADD(MINUTE,25,GETDATE())
SELECT CAST(@date2 - @date AS TIME)
于 2013-09-11T19:38:40.087 回答
1
DATEDIFF(HH,BOOKDATIME, BOOKDATIME_OFF)
于 2013-09-11T19:37:50.463 回答