Find difference between timestamps in seconds in PostgreSQL

Postgresql

Postgresql Problem Overview


I have a table in PostgreSQL 8.3 with 2 timestamp columns. I would like to get the difference between these timestamps in seconds. Could you please help me how to get this done?

TableA
(
  timestamp_A timestamp,
  timestamp_B timestamp
)

I need to get something like (timestamo_B - timestamp_A) in seconds (not just the difference between seconds, it should include hours, minutes etc).

Postgresql Solutions


Solution 1 - Postgresql

Try: 

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

Details here: EXTRACT.

Solution 2 - Postgresql

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArunView Question on Stackoverflow
Solution 1 - PostgresqlIhor RomanchenkoView Answer on Stackoverflow
Solution 2 - PostgresqlClodoaldo NetoView Answer on Stackoverflow