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

后端 未结 6 1450
无人及你
无人及你 2021-01-01 12:25

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

Since

sum(time.endtime-time.starttime)

returns the interval as \"1 da

相关标签:
6条回答
  • 2021-01-01 12:53

    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

    0 讨论(0)
  • 2021-01-01 12:54

    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.

    0 讨论(0)
  • 2021-01-01 12:56

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

    0 讨论(0)
  • 2021-01-01 13:06

    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.

    0 讨论(0)
  • 2021-01-01 13:06

    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
    
    0 讨论(0)
  • 2021-01-01 13:11

    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.

    0 讨论(0)
提交回复
热议问题