DateDiff() Hours and Minutes? [duplicate]

我的梦境 提交于 2019-12-24 15:42:47

问题


This question have been asked many times but i cannot find any easy answers on how to get hours and minutes from a datediff().

From    To  OUTPUT
08:00   16:30   8,5
10:00   16:30   6,5
08:00   15:00   7

I would like to use datediff() and want the output as my result


回答1:


just take datediff() by minute and then divide by 60

select  datediff(minute, '08:00', '16:30') / 60.0



回答2:


Sorry, I answered C# question first :)

What you're after is this:

SELECT datediff(minute, starttime, endtime) from ...

of course, you can apply this to hours too. For Hours AND minutes, you can use this example:

DECLARE @start datetime
      , @end   datetime

SELECT @start = '2009-01-01'
     , @end   = DateAdd(mi, 52, DateAdd(hh, 18, DateAdd(dd, 2, @start)))

SELECT @start
 , @end

SELECT DateDiff(dd, @start, @end)      As days
     , DateDiff(hh, @start, @end) % 24 As hours
     , DateDiff(mi, @start, @end) % 60 As mins



回答3:


I went with

DATEDIFF(second, start_date, end_date) / 3600.0          

Thank you all for the answers...!!.



来源:https://stackoverflow.com/questions/35840300/datediff-hours-and-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!