sqlsql-servert-sqlsql-server-2017

SQL Convert Milliseconds to Days, Hours, Minutes


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#?


Solution

  • 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 %.