DateDiff to output hours and minutes

后端 未结 12 1962
半阙折子戏
半阙折子戏 2020-12-09 09:47

my code gives TOTAL HOURS in hours, but i am trying to output something like

TotalHours 
  8:36

where 8 represents hour part and 36 repres

相关标签:
12条回答
  • 2020-12-09 10:33

    Small change like this can be done

      SELECT  EmplID
            , EmplName
            , InTime
            , [TimeOut]
            , [DateVisited]
            , CASE WHEN minpart=0 
            THEN CAST(hourpart as nvarchar(200))+':00' 
            ELSE CAST((hourpart-1) as nvarchar(200))+':'+ CAST(minpart as nvarchar(200))END as 'total time'
            FROM 
            (
            SELECT   EmplID, EmplName, InTime, [TimeOut], [DateVisited],
            DATEDIFF(Hour,InTime, [TimeOut]) as hourpart, 
            DATEDIFF(minute,InTime, [TimeOut])%60 as minpart  
            from times) source
    
    0 讨论(0)
  • 2020-12-09 10:33

    Divide the Datediff in MS by the number of ms in a day, cast to Datetime, and then to time:

    Declare @D1 datetime = '2015-10-21 14:06:22.780', @D2 datetime = '2015-10-21 14:16:16.893'
    
    Select  Convert(time,Convert(Datetime, Datediff(ms,@d1, @d2) / 86400000.0))
    
    0 讨论(0)
  • 2020-12-09 10:36

    Since any DateTime can be cast to a float, and the decimal part of the number represent the time itself:

    DECLARE @date DATETIME = GETDATE()
    
    SELECT CAST(CAST(@date AS FLOAT) - FLOOR(CAST(@date AS FLOAT)) AS DATETIME
    

    This will result a datetime like '1900-01-01 hour of the day' you can cast it as time, timestamp or even use convert to get the formatted time.

    I guess this works in any version of SQL since cast a datetime to float is compatible since version 2005.

    Hope it helps.

    0 讨论(0)
  • 2020-12-09 10:40

    Just change the

    DATEDIFF(Hour,InTime, [TimeOut]) TotalHours
    

    part to

    CONCAT((DATEDIFF(Minute,InTime,[TimeOut])/60),':',
           (DATEDIFF(Minute,InTime,[TimeOut])%60)) TotalHours 
    

    The /60 gives you hours, the %60 gives you the remaining minutes, and CONCAT lets you put a colon between them.

    I know it's an old question, but I came across it and thought it might help if someone else comes across it.

    0 讨论(0)
  • 2020-12-09 10:40

    If you want 08:30 ( HH:MM) format then try this,

    SELECT EmplID
        , EmplName
        , InTime
        , [TimeOut]
        , [DateVisited]
        ,  RIGHT('0' + CONVERT(varchar(3),DATEDIFF(minute,InTime, TimeOut)/60),2) + ':' +
          RIGHT('0' + CONVERT(varchar(2),DATEDIFF(minute,InTime,TimeOut)%60),2)
          as TotalHours from times Order By EmplID, DateVisited
    
    0 讨论(0)
  • 2020-12-09 10:41

    I would make your final select as:

    SELECT    EmplID
            , EmplName
            , InTime
            , [TimeOut]
            , [DateVisited]
            , CONVERT(varchar(3),DATEDIFF(minute,InTime, TimeOut)/60) + ':' +
              RIGHT('0' + CONVERT(varchar(2),DATEDIFF(minute,InTime,TimeOut)%60),2)
              as TotalHours
    from times
    Order By EmplID, DateVisited 
    

    Any solution trying to use DATEDIFF(hour,... is bound to be complicated (if it's correct) because DATEDIFF counts transitions - DATEDIFF(hour,...09:59',...10:01') will return 1 because of the transition of the hour from 9 to 10. So I'm just using DATEDIFF on minutes.

    The above can still be subtly wrong if seconds are involved (it can slightly overcount because its counting minute transitions) so if you need second or millisecond accuracy you need to adjust the DATEDIFF to use those units and then apply suitable division constants (as per the hours one above) to just return hours and minutes.

    0 讨论(0)
提交回复
热议问题