DATEDIFF in HH:MM:SS format

前端 未结 6 977
予麋鹿
予麋鹿 2020-12-06 10:29

I need to calculate the total length in terms of Hours, Minutes, Seconds, and the average length, given some data with start time and end time.

For example the resul

6条回答
  •  鱼传尺愫
    2020-12-06 11:15

    I slightly modified Avinash's answer as it may end with error if difference is too big. If you need only HH:mm:ss it is sufficient to distinguish at seconds level ony like this:

    SELECT CONVERT(time, 
      DATEADD(s, 
        DATEDIFF(s, 
          '2018-01-07 09:53:00', 
          '2018-01-07 11:53:01'), 
         CAST('1900-01-01 00:00:00.0000000' as datetime2)
       )
    )
    

提交回复
热议问题