Let’s say you get a number of seconds and want to convert it to a string so that it’s human readable e.g. saying that something took 15157 seconds is probably not a good idea and it’d make more sense to say it took 4 hours 12 minutes and 37 seconds.
Here’s a very simple stored procedure to perform this conversion:
create procedure hours_min_sec(@seconds int) as begin select right('00'+convert(varchar(2), floor(@seconds / 3600)), 2)+':'+right('00'+convert(varchar(2),floor(@seconds / 60) % 60), 2)+':'+right('00'+convert(varchar(2),@seconds % 60), 2) end go
Note that the you may see a line wrap here but actually everything after begin and before end should be on one line.
You can use it like this:
1> hours_min_sec 15157 2> go -------- 04:12:37 (1 row affected) (return status = 0)