DateDiff to output hours and minutes

后端 未结 12 1961
半阙折子戏
半阙折子戏 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:14

    Try this query

    select
        *,
        Days          = datediff(dd,0,DateDif),
        Hours         = datepart(hour,DateDif),
        Minutes       = datepart(minute,DateDif),
        Seconds       = datepart(second,DateDif),
        MS            = datepart(ms,DateDif)
    from
        (select
             DateDif = EndDate-StartDate,
             aa.*
         from
             (  -- Test Data
              Select
                  StartDate = convert(datetime,'20090213 02:44:37.923'),
                  EndDate   = convert(datetime,'20090715 13:24:45.837')) aa
        ) a
    

    Output

    DateDif                  StartDate                EndDate                 Days Hours Minutes Seconds MS
    -----------------------  -----------------------  ----------------------- ---- ----- ------- ------- ---
    1900-06-02 10:40:07.913  2009-02-13 02:44:37.923  2009-07-15 13:24:45.837 152  10    40      7       913
    
    (1 row(s) affected)
    
    0 讨论(0)
  • 2020-12-09 10:18

    Very simply:

    CONVERT(TIME,Date2 - Date1)
    

    For example:

    Declare @Date2 DATETIME = '2016-01-01 10:01:10.022'
    Declare @Date1 DATETIME = '2016-01-01 10:00:00.000'
    Select CONVERT(TIME,@Date2 - @Date1) as ElapsedTime
    

    Yelds:

    ElapsedTime
    ----------------
    00:01:10.0233333
    
    (1 row(s) affected)
    
    0 讨论(0)
  • 2020-12-09 10:18

    Please put your related value and try this :

    declare @x int, @y varchar(200),
            @dt1 smalldatetime = '2014-01-21 10:00:00', 
            @dt2 smalldatetime = getdate()
    
    set @x = datediff (HOUR, @dt1, @dt2)
    set @y =  @x * 60 -  DATEDIFF(minute,@dt1, @dt2)
    set @y = cast(@x as varchar(200)) + ':' + @y
    Select @y
    
    0 讨论(0)
  • 2020-12-09 10:22

    In case someone is still searching for a query to display the difference in hr min and sec format: (This will display the difference in this format: 2 hr 20 min 22 secs)

    SELECT
    CAST(DATEDIFF(minute, StartDateTime, EndDateTime)/ 60 as nvarchar(20)) + ' hrs ' + CAST(DATEDIFF(second, StartDateTime, EndDateTime)/60 as nvarchar(20)) + ' mins' +          CAST(DATEDIFF(second, StartDateTime, EndDateTime)% 60 as nvarchar(20))  + ' secs'
    

    OR can be in the format as in the question:

    CAST(DATEDIFF(minute, StartDateTime, EndDateTime)/ 60 as nvarchar(20)) + ':' + CAST(DATEDIFF(second, StartDateTime, EndDateTime)/60 as nvarchar(20))
    
    0 讨论(0)
  • 2020-12-09 10:23

    this would hep you

     DECLARE @DATE1 datetime = '2014-01-22 9:07:58.923'
     DECLARE @DATE2 datetime = '2014-01-22 10:20:58.923'
     SELECT DATEDIFF(HOUR, @DATE1,@DATE2) ,
            DATEDIFF(MINUTE, @DATE1,@DATE2) - (DATEDIFF(HOUR,@DATE1,@DATE2)*60)
    
     SELECT CAST(DATEDIFF(HOUR, @DATE1,@DATE2) AS nvarchar(200)) +
            ':'+ CAST(DATEDIFF(MINUTE, @DATE1,@DATE2)  -
                     (DATEDIFF(HOUR,@DATE1,@DATE2)*60) AS nvarchar(200))
    As TotalHours 
    
    0 讨论(0)
  • 2020-12-09 10:25

    No need to jump through hoops. Subtracting Start from End essentially gives you the timespan (combining Vignesh Kumar's and Carl Nitzsche's answers) :

    SELECT *,
        --as a time object
        TotalHours = CONVERT(time, EndDate - StartDate),
        --as a formatted string
        TotalHoursText = CONVERT(varchar(20), EndDate - StartDate, 114)
    FROM (
        --some test values (across days, but OP only cares about the time, not date)
        SELECT
            StartDate = CONVERT(datetime,'20090213 02:44:37.923'),
            EndDate   = CONVERT(datetime,'20090715 13:24:45.837')
    ) t
    

    Ouput

    StartDate               EndDate                 TotalHours       TotalHoursText
    ----------------------- ----------------------- ---------------- --------------------
    2009-02-13 02:44:37.923 2009-07-15 13:24:45.837 10:40:07.9130000 10:40:07:913
    

    See the full cast and convert options here: https://msdn.microsoft.com/en-us/library/ms187928.aspx

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