简体   繁体   中英

How to convert an interval like “1 day 01:30:00” into “25:30:00”?

I need to add some intervals and use the result in Excel.

Since

sum(time.endtime-time.starttime)

returns the interval as "1 day 01:30:00" and this format breaks my Excel sheet, I thought it'd be nice to have the output like "25:30:00" but found no way to do it in the PostgreSQL documentation.

Can anyone here help me out?

The only thing I can come with (beside parsing the number of days and adding 24 to the hours every time) is :

mat=> select date_part('epoch', '01 day 1:30:00'::interval);
 date_part 
-----------
     91800
(1 row)

It will give you the number of seconds, which may be ok for excel.

Since there is not an exact solution for the topic:

=> SELECT date_part('epoch', INTERVAL '1 day 01:30:00') * INTERVAL '1 second' hours;
  hours
-----------
 25:30:00
(1 row)

Source: Documentation

You could use EXTRACT to convert the interval into seconds.

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
Result: 442800

Then you would need to do your own maths (or let Excel do it).

Note that '1 day' is not necessarily equivalent to '24 hours' - PostgreSQL handles things like an interval that spans a DST transition.

If you wanted postgres to handle the HH:MM:SS formatting for you, take the difference in epoch seconds and convert it to an interval scaled in seconds:

SELECT SUM(EXTRACT(EPOCH FROM time.endtime) - EXTRACT(EPOCH FROM time.starttime))
         * INTERVAL '1 SECOND' AS hhmmss

In standard SQL, you want to represent the type as INTERVAL HOUR TO SECOND, but you have a value of type INTERVAL DAY TO SECOND. Can you not use a CAST to get to your required result? In Informix, the notation would be either of:

SUM(time.endtime - time.starttime)::INTERVAL HOUR(3) TO SECOND

CAST(SUM(time.endtime - time.starttime) AS INTERVAL HOUR(3) TO SECOND)

The former is, AFAIK, Informix-specific notation (or, at least, not standard); the latter is, I believe, SQL standard notation.

It can be done, but I believe that the only way is through the following monstrosity (assuming your time interval column name is "ti"):

select
              to_char(floor(extract(epoch from ti)/3600),'FM00')
    || ':' || to_char(floor(cast(extract(epoch from ti) as integer) % 3600 / 60), 'FM00')
    || ':' || to_char(cast(extract(epoch from ti) as integer) % 60,'FM00')
    as hourstamp
from whatever;

See? I told you it was horrible :)

It would have been nice to think that

select to_char(ti,'HH24:MI:SS') as hourstamp from t

would worked, but alas, the HH24 format doesn't "absorb" the overflow beyond 24. The above comes (reconstructed from memory) from some code I once wrote. To avoid offending those of delicate constitution, I encapsulated the above shenanigans in a view...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM