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
The situation is you have a value in Seconds (XXX.XX), and you want to convert to HH:MM:SS using T-SQL.
Example:
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)
Using SQL Server 05 I can get this to work by using:
declare @OrigValue int;
set @OrigValue = 121.25;
select replace(str(@OrigValue/3600,len(ltrim(@OrigValue/3600))+abs(sign(@OrigValue/359999)-1)) + ':' + str((@OrigValue/60)%60,2) + ':' + str(@OrigValue%60,2),' ','0')
Just in case this might be still interesting to anyone. The 'Format' Function can also be used, with SQL Server 2012+
Declare @Seconds INT = 1000000; SELECT FORMAT(CAST(@Seconds/86400.000 AS datetime), 'HH:mm:ss');
OR
Declare @Seconds INT = 1000000; SELECT CAST(FORMAT(CAST(@Seconds/86400.000 AS datetime), 'HH:mm:ss') AS TIME);
DECLARE @Seconds INT = 86200;
SELECT
CONVERT(VARCHAR(15),
CAST(CONVERT(VARCHAR(12), @Seconds / 60 / 60 % 24)
+':'+ CONVERT(VARCHAR(2), @Seconds / 60 % 60)
+':'+ CONVERT(VARCHAR(2), @Seconds % 60) AS TIME), 100) AS [HH:MM:SS (AM/PM)]
You can try this
set @duration= 112000
SELECT
"Time" = cast (@duration/3600 as varchar(3)) +'H'
+ Case
when ((@duration%3600 )/60)<10 then
'0'+ cast ((@duration%3600 )/60)as varchar(3))
else
cast ((@duration/60) as varchar(3))
End
I use this:
cast(datediff(hh, '1900-01-01', dateadd(s, @Seconds), 0)) as varchar(10))
+ right(convert(char(8), dateadd(s, @Seconds), 0), 108),6) AS [Duration(H:MM:SS)]
CAST((Duration)/60 AS NVARCHAR(9)) + ':' + RIGHT('00' + CAST(Duration % 60 AS NVARCHAR(9)), 2)