SQL Convert Milliseconds to Days, Hours, Minutes

sd_dracula picture sd_dracula · Jul 17, 2017 · Viewed 24.6k times · Source

I need convert a millisecond value, 85605304.3587 to a value like 0d 18h 21m. No idea on how to start that, is there something similar to a TimeSpan in SQL like there is in C#?

Answer

Gordon Linoff picture Gordon Linoff · Jul 17, 2017

You can do the calculation explicitly. I think it is:

select floor(msvalue / (1000 * 60 * 60 * 24)) as days,
       floor(msvalue / (1000 * 60 * 60)) % 24 as hours,
       floor(msvalue / (1000 * 60)) % 60 as minutes

Note: Some databases use mod instead of %.