Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Seconds to HH:MM:SS Revisited

Date and time Tips and tricks

Converting Seconds to HH:MM:SS Revisited

by  AlexCuse  Posted    (Edited  )
As pointed out by a helpful member (and in my estimation date/time wizard) I was going about this all wrong. The easiest (and IMO best) way to do this is as follows:

Code:
create function SecTimeDay (@sec integer) 
returns varchar(19)
as
begin

declare @DayTime varchar(19)

/* Calculate # of days and display if necessary */
Select @DayTime = Case When @Sec >= 86400
            	Then Convert(VarChar(5), @Sec/86400) 
                + ' days ' 
            	Else ''
            	End
/* Add HH:MM:SS to number of days (or ') for output */
       + Convert(VarChar(8), DateAdd(Second, @Sec, 0), 108)

return @DayTime

end

This function performs much better than the one I initially posted, and it also reports days.

Hope this one is helpful to someone.

Alex


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top