Archive
Posts Tagged ‘SQL DateTime’
SQL Trivia – How to convert milliseconds to [hh:mm:ss.ms] format
August 1, 2017
1 comment
Today for some reporting purpose I need to convert milliseconds to hh:mm:ss.ms format, i.e.
Hours : Minutes : Seconds . Micro-Seconds
So, I tried to create query below, of course by taking help from internet, so that I can have a sample code handy for future reference:
DECLARE @MilliSeconds INT
SET @MilliSeconds = 25289706
SELECT CONCAT(
RIGHT('0' + CAST(@MilliSeconds/(1000*60*60) AS VARCHAR(2)),2), ':', -- Hrs
RIGHT('0' + CAST((@MilliSeconds%(1000*60*60))/(1000*60) AS VARCHAR(2)),2), ':', -- Mins
RIGHT('0' + CAST(((@MilliSeconds%(1000*60*60))%(1000*60))/1000 AS VARCHAR(2)),2), '.', -- Secs
((@MilliSeconds%(1000*60*60))%(1000*60))%1000 -- Milli Secs
) AS [hh:mm:ss.ms]
-- 7 Hrs, 1 minute, 29 seconds and 706 milliseconds

Categories: SQL Trivia
Convert Date, SQL DateTime




