Sample Header Ad - 728x90

number of seconds to years-months-weeks-days-HH:MM-SS

1 vote
2 answers
2309 views
I am running this all the time to get HH:MM:SS from a number of seconds: DECLARE @s INT SELECT @s = 23251 SELECT @s , CONVERT(TIME, DATEADD(SECOND, @s, 0)); enter image description here when I want to add days to the equation I run the following query: DECLARE @seconds AS int = 232511; SELECT CONVERT(varchar, (@seconds / 86400)) --Days + ':' + CONVERT(varchar, DATEADD(ss, @seconds, 0), 108); --Hours, Minutes, Seconds enter image description here what if I wanted as well the number of weeks, months, and years? How would I go around that? I think the exact calculation would need to have a reference to when the number of seconds happened. Let's say for instance I am interested in how long a sql server job has been running for. I have jobs within the replication category that have been running for years, and the starting point is known. When not specified we could assume the number of seconds are up to now.
Asked by Marcello Miorelli (17274 rep)
Sep 21, 2016, 01:50 PM
Last activity: Apr 21, 2020, 01:46 PM