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

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

提交回复
热议问题