How to convert number of minutes to hh:mm format in TSQL?

前端 未结 12 1945
情话喂你
情话喂你 2020-12-03 09:48

I have a select query that has DURATION column to calculate number of Minutes . I want to convert those minutes to hh:mm format.

相关标签:
12条回答
  • 2020-12-03 10:20

    In case someone is interested in getting results as 60 becomes 01:00 hours, 120 becomes 02:00 hours, 150 becomes 02:30 hours, this function might help:

        create FUNCTION [dbo].[MinutesToHHMM]
    (
        @minutes int 
    )
    RETURNS varchar(30)
    AS
    
    BEGIN
    declare @h int
            set @h= @minutes / 60
            declare @mins varchar(2)
            set @mins= iif(@minutes%60<10,concat('0',cast((@minutes % 60) as varchar(2))),cast((@minutes % 60) as varchar(2)))
    return iif(@h <10, concat('0', cast(@h as varchar(5)),':',@mins)
            ,concat(cast(@h as varchar(5)),':',@mins))
    
    end
    
    0 讨论(0)
  • 2020-12-03 10:20

    How to get the First and Last Record time different in sql server....

    ....

    Select EmployeeId,EmployeeName,AttendenceDate,MIN(Intime) as Intime ,MAX(OutTime) as OutTime,
    DATEDIFF(MINUTE, MIN(Intime), MAX(OutTime)) as TotalWorkingHours
    FROM ViewAttendenceReport WHERE AttendenceDate >='1/20/2020 12:00:00 AM' AND AttendenceDate <='1/20/2020 23:59:59 PM' 
    GROUP BY EmployeeId,EmployeeName,AttendenceDate;
    
    
    0 讨论(0)
  • 2020-12-03 10:20

    This seems to work for me:

    SELECT FORMAT(@mins / 60 * 100 + @mins % 60, '#:0#')

    0 讨论(0)
  • 2020-12-03 10:23
    select convert(varchar(5),dateadd(mi,DATEDIFF(minute, FirstDate,LastDate),'00:00'),114)    
    
    0 讨论(0)
  • 2020-12-03 10:29

    I would do the following (copy-paste the whole stuff below into immediate window / query window and execute)

    DECLARE @foo int
    DECLARE @unclefoo smalldatetime
    SET @foo = DATEDIFF(minute, CAST('2013.01.01 00:00:00' AS datetime),CAST('2013.01.01 00:03:59' AS datetime)) -- AS 'Duration (Minutes)'
    
    SET @unclefoo = DATEADD(minute, @foo, '2000.01.01')
    
    SELECT CAST(@unclefoo AS time)
    

    @foo stores the value you generate in your question. The "trick" comes by then:
    we create a smalldatetime variable (in my case it's yyyy.mm.dd format) and increment it with your int value, then display (or store if you want) the time part only.

    0 讨论(0)
  • 2020-12-03 10:30

    Thanks to A Ghazal, just what I needed. Here's a slightly cleaned up version of his(her) answer:

    create FUNCTION [dbo].[fnMinutesToDuration]
    (
        @minutes int 
    )
    RETURNS nvarchar(30)
    
    -- Based on http://stackoverflow.com/questions/17733616/how-to-convert-number-of-minutes-to-hhmm-format-in-tsql
    
    AS
    
    BEGIN
    
    return rtrim(isnull(cast(nullif((@minutes / 60)
                                    , 0
                                   ) as varchar
                            ) + 'h '
                        ,''
                       )
                + isnull(CAST(nullif((@minutes % 60)
                                     ,0
                                    ) AS VARCHAR(2)
                             ) + 'm'
                         ,''
                        )
                )
    
    end
    
    0 讨论(0)
提交回复
热议问题