In order to get the difference between two dates or datetime(timestamp) in seconds in PostgreSQL, We will be using simple difference symbol (subtraction). Let’s see an example of all the approaches. We will also consider an example on how to get difference between two dates or datetime(timestamp) column in seconds in PostgreSQL table.
Difference between two datetime (timestamp) in Seconds PostgreSQL using simple difference:
Simple Difference symbol is used to compute the difference between two datetime (timestamp) in seconds (multiply by 86400) as shown below.
Example 1:
select *,round(abs(birthdaytime :: date - examdatetime :: date)*(86400),2) as seconds_diff from student_detail2;
Difference between two datetime (timestamp) in columns in seconds PostgreSQL :
Method 1
We will be using below Student_detail2 table for our example to depict on how to find difference between two datetime (timestamp) columns in seconds in postgresql.
Student_detail2:
Simple Difference symbol is used to compute the difference between two date column in the PostgreSQL table in seconds as shown below.
select *,round(abs(birthdaytime :: date - examdatetime :: date)*(86400),2) as Seconds_diff from student_detail2;
We have typecasted the two date columns to date and then found the difference and converted the difference in seconds, so it will be rounded off