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

前端 未结 12 1944
情话喂你
情话喂你 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:11

    I'm not sure these are the best options but they'll definitely get the job done:

    declare @durations table
    (
        Duration int
    )
    
    Insert into @durations(Duration)
    values(60),(80),(90),(150),(180),(1000)
    
    --Option 1 - Manually concatenate the values together
    select right('0' + convert(varchar,Duration / 60),2) + ':' + right('0' + convert(varchar,Duration % 60),2)
    from @Durations
    
    --Option 2 - Make use of the time variable available since SQL Server 2008
    select left(convert(time,DATEADD(minute,Duration,0)),5)
    from @durations
    
    GO
    
    0 讨论(0)
  • 2020-12-03 10:12
    declare function dbo.minutes2hours (
        @minutes int
    )
    RETURNS varchar(10)
    as
    begin
        return format(dateadd(minute,@minutes,'00:00:00'), N'HH\:mm','FR-fr')
    end
    
    0 讨论(0)
  • 2020-12-03 10:14

    For those who need convert minutes to time with more than 24h format:

    DECLARE @minutes int = 7830
    SELECT CAST(@minutes / 60 AS VARCHAR(8)) + ':' + FORMAT(@minutes % 60, 'D2') AS [Time]
    

    Result:

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

    You can convert the duration to a date and then format it:

    DECLARE
        @FirstDate datetime,
        @LastDate datetime
    
    SELECT
        @FirstDate = '2000-01-01 09:00:00',
        @LastDate = '2000-01-01 11:30:00'
    
    SELECT CONVERT(varchar(12), 
           DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114) 
    
    /* Results: 02:30:00:000 */
    

    For less precision, modify the size of the varchar:

    SELECT CONVERT(varchar(5), 
           DATEADD(minute, DATEDIFF(minute, @FirstDate, @LastDate), 0), 114) 
    
    /* Results: 02:30 */
    
    0 讨论(0)
  • 2020-12-03 10:16
    DECLARE @Duration int
    
    SET @Duration= 12540 /* for example big hour amount in minutes -> 209h */
    
    SELECT CAST( CAST((@Duration) AS int) / 60 AS varchar) + ':'  + right('0' + CAST(CAST((@Duration) AS int) % 60 AS varchar(2)),2)
    
    /* you will get hours and minutes divided by : */
    
    0 讨论(0)
  • 2020-12-03 10:17

    This function is to convert duration in minutes to readable hours and minutes format. i.e 2h30m. It eliminates the hours if the duration is less than one hour, and shows only the hours if the duration in hours with no extra minutes.

    CREATE FUNCTION [dbo].[MinutesToDuration]
    (
        @minutes int 
    )
    RETURNS nvarchar(30)
    
    AS
    BEGIN
    declare @hours  nvarchar(20)
    
    SET @hours = 
        CASE WHEN @minutes >= 60 THEN
            (SELECT CAST((@minutes / 60) AS VARCHAR(2)) + 'h' +  
                    CASE WHEN (@minutes % 60) > 0 THEN
                        CAST((@minutes % 60) AS VARCHAR(2)) + 'm'
                    ELSE
                        ''
                    END)
        ELSE 
            CAST((@minutes % 60) AS VARCHAR(2)) + 'm'
        END
    
    return @hours
    END
    

    To use this function :

    SELECT dbo.MinutesToDuration(23)
    

    Results: 23m

    SELECT dbo.MinutesToDuration(120)
    

    Results: 2h

    SELECT dbo.MinutesToDuration(147)
    

    Results: 2h27m

    Hope this helps!

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