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));
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
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
.
Best Answer
Assuming you know
@Seconds
and@StartDateTime
, something like the following could work: