SQL Convert Milliseconds to Days, Hours, Minutes
Asked Answered
M

3

15

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

Madcap answered 17/7, 2017 at 14:23 Comment(5)
Which DBMS are you using? Postgres? Oracle? The data type in standard SQL (which is what the sql tag refers to) would be intervalInfuse
SQL 2017 is what I am usingMadcap
There is no standard named "SQL 2017" but I guess you meant "SQL Server 2017"Infuse
The equivalent to TimeSpan is time but it stores only the time of day, not a duration. That means you can't specify times greater than 24 hours. TimeSpan can be stored directly into a time fieldDrawing
Where does this value come from? What is its range? Why not store it into a time field directly?Drawing
S
27

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

Stationmaster answered 17/7, 2017 at 14:31 Comment(1)
1st bracket in 3rd line makes this unrunnable.Gord
C
2

In MS SQL SERVER you can use next code:

with cte as (
    select cast(85605304.3587 as int) / 1000 / 60 as [min]
), cte2 as (
    select 
        cast([min] % 60 as varchar(max)) as minutes,
        cast(([min] / 60) % 24 as varchar(max)) as hours,
        cast([min] / (60 * 24) as varchar(max)) as days
    from cte
)
select concat(days, 'd ', hours, 'h ', minutes, 'm') as tm
from cte2
Contralto answered 17/7, 2017 at 14:38 Comment(0)
G
2

Using native date & time functions, maybe:

SELECT
    AsDateTime = DATEADD(MILLISECOND, 85605304, 0)
  , AsDateTime2 = DATEADD(NANOSECOND, 7 * 100, DATEADD(MICROSECOND, 358, DATEADD(MILLISECOND, 85605304, CONVERT(datetime2, CONVERT(datetime, 0)))))
-- Incorrect datetime2 approach I initially did, has some precision loss, probably due to datetime's millisecond issue with 0's, 3's, and 7.'s
--SELECT DontDoThis = DATEADD(NANOSECOND, 7 * 100, DATEADD(MICROSECOND, 358, CONVERT(datetime2, DATEADD(MILLISECOND, 85605304, 0))))

datetime covers only 3 digits beyond seconds, while datetime2 will maintain 7 digits. Perhaps other ways that give date-like objects exist, I wouldn't know.

Gord answered 17/7, 2017 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.