I have a select query that has DURATION
column to calculate number of Minutes . I want to convert those minutes to hh:mm
format.>
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