Is there any tricky way to format seconds like hours:minutes:seconds. For example,
3660
seconds will be displayed as
01h 01m 00s
or
01:01:00
I am aware of the standard way of doing this:
- Divide all seconds on 3600 to get the hours
- Divide the rest seconds on 60 to get the minutes
- The rest are the seconds
I met the following issues:
-
I am not able to create separate function that do this.
-
My code is in view using several CTEs. So, variables can be declare
using the CTEs only. - I am not able to use the standard solution because I will have
results bigger then one day – How to convert Seconds to HH:MM:SS using T-SQL
Result:
See this SQLFiddle
Update
The above query works fine if the total number of hours are less than 100 i.e. (99:59:59). If you need more than that you can use the following query:
Result:
See this SQLFiddle