Format hh:mm with SQL Server
-
04-12-2019 - |
Question
I'm using
CONVERT( CHAR(5), DATEADD(n, value),0), 108)
to convert minutes to a hh:mm
format, but it breaks when I have more minutes then fit in 24hrs. E.g. 1440
gives 00:00
instead of 24:00
.
How can I fix that?
Solution
Consider using the %
modulo operator:
select cast(n/60 as varchar(20)) + ':' +
right('0' + cast(n%60 as varchar(2)), 2)
OTHER TIPS
Try something like this:
declare @minutes int
select @minutes = 1440
select convert(varchar, @minutes/60) + ':' + right ('0'+convert(varchar, @minutes%60), 2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow