February 2, 2009 at 11:12 am
how to convert seconds to HH:MM:SS format?
February 2, 2009 at 11:22 am
Let me know if this fits your bill:
declare @secs tinyint,
@mins tinyint,
@hrs smallint,
@seconds int;
set @seconds = 1234567;
set @secs = @seconds % 60;
set @mins = (@seconds / 60) % 60;
set @hrs = (@seconds / 60) / 60;
select
@seconds,
@hrs,
@mins,
@secs,
cast(@hrs as varchar(8)) + ':' + right('0' + cast(@mins as varchar(2)), 2) + ':' + right('0' + cast(@secs as varchar(2)), 2);
February 2, 2009 at 11:26 am
it's kind of easy...
in your case, you only care about the time portion of any date....not the date itself.
so you ADD the number of seconds to an arbitrary date,
then one of the CONVERT functions paramneters allowed you to get just the TIME portion of a specified date:
SELECT DATEADD(month, 147859, '2006-08-31') --adding a bunch of seconds to any ol date
Results:2006-09-01 17:04:19.000
SELECT CONVERT(VARCHAR,DATEADD(second, 147859, '2006-08-31') ,114) --selecting that same date, and formatting it to HH::MM:SS:nnnn
Results:17:04:19:000
Lowell
February 2, 2009 at 12:37 pm
CONVERT(VARCHAR(8), DATEADD(ss, 12345, 0) ,114) will cut off milliseconds.
_____________
Code for TallyGenerator
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply