Find difference between timestamps in seconds in PostgreSQL

后端 未结 3 1716
遇见更好的自我
遇见更好的自我 2020-12-02 08:57

I have a table in PostgreSQL 8.3 with 2 timestamp columns. I would like to get the difference between these timestamps in seconds. Cou

相关标签:
3条回答
  • 2020-12-02 09:20
    select age(timestamp_A, timestamp_B)
    

    Answering to Igor's comment:

    select age('2013-02-28 11:01:28'::timestamp, '2011-12-31 11:00'::timestamp);
                  age              
    -------------------------------
     1 year 1 mon 28 days 00:01:28
    
    0 讨论(0)
  • 2020-12-02 09:24
    SELECT (cast(timestamp_1 as bigint) - cast(timestamp_2 as bigint)) FROM table;
    

    In case if someone is having an issue using extract.

    0 讨论(0)
  • 2020-12-02 09:32

    Try: 

    SELECT EXTRACT(EPOCH FROM (timestamp_B - timestamp_A))
    FROM TableA
    

    Details here: EXTRACT.

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