Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Seconds to HH:MM:SS using T-SQL

Tags:

datetime

tsql

The situation is you have a value in Seconds (XXX.XX), and you want to convert to HH:MM:SS using T-SQL.

Example:

  • 121.25 s becomes 00:02:01.25
like image 423
Brett Veenstra Avatar asked Aug 11 '09 19:08

Brett Veenstra


People also ask

How do you convert seconds to HH MM SS in SQL?

SELECT SEC_TO_TIME( seconds ); SELECT SEC_TO_TIME( seconds ); In this query, the “seconds” parameter is the number of seconds to convert. The result will be displayed in “HH:MM:SS” format.

How do you get HH MM SS from seconds?

To convert seconds to HH:MM:SS :Multiply the seconds by 1000 to get milliseconds. Pass the milliseconds to the Date() constructor. Use the toISOString() method on the Date object.


1 Answers

You want to multiply out to milliseconds as the fractional part is discarded.

SELECT DATEADD(ms, 121.25 * 1000, 0) 

If you want it without the date portion you can use CONVERT, with style 114

SELECT CONVERT(varchar, DATEADD(ms, 121.25 * 1000, 0), 114) 
like image 134
great_llama Avatar answered Dec 06 '22 06:12

great_llama