Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate the time between two timestamps (PostgreSQL)

Calculate the time between these two timestamps in PostgreSQL,
create_time='2017-11-02 05:51:13' and update_time='2017-11-02 07:36:18'
and display it on HH:MM:SS Format. it should display like this 01:45:04


1 Answers

You can try just subtracting the two timestamps, then using to_char to extract out the time portion:

select
    SELECT to_char('2017-11-02 07:36:18'::timestamp -
                   '2017-11-02 05:51:10'::timestamp, 'HH:MI:SS');

Output:

to_char
01:45:08

Demo

like image 141
Tim Biegeleisen Avatar answered Oct 31 '25 05:10

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!