Difference between two datetime (timestamp) in milliseconds and microseconds – PostgreSQL

In order to get the difference between two dates or datetime(timestamp) in milliseconds or microseconds 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 milliseconds and microseconds in PostgreSQL table.

 

Difference between two datetime (timestamp) in milliseconds PostgreSQL using simple difference:

Simple Difference symbol is used to compute the difference between two datetime (timestamp) in milliseconds (multiply by 8.64e+7) as shown below.

Example 1:


SELECT round(abs('2024-04-01 09:12:00' :: date - '2020-04-14 09:12:00' :: date)*(8.64e+7),2) as milliseconds_diff;

 output:

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-1

 

Difference between two datetime (timestamp) in microseconds PostgreSQL using simple difference:

Simple Difference symbol is used to compute the difference between two datetime (timestamp) in microseconds (multiply by 8.64e+10) as shown below.

Example 1:


SELECT round(abs('2024-04-01 09:12:00' :: date - '2020-04-14 09:12:00' :: date)*(8.64e+10),2) as microseconds_diff;

 output:

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-2

 

 

 

Difference between two datetime (timestamp) in columns in milliseconds 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 milliseconds in postgresql.

Student_detail2:

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-3

Simple Difference symbol is used to compute the difference between two date column in the PostgreSQL table and its is multiplied by  8.64e+7 to convert results into milliseconds as shown below.


select *,round(abs(birthdaytime :: date - examdatetime :: date)*(8.64e+7),2) as milliseconds_diff from student_detail2;

We have typecasted the two date columns to date and then found the difference and converted the difference in milliseconds.

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-4

 

 

Difference between two datetime (timestamp) in columns in microseconds 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 microseconds in postgresql.

Student_detail2:

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-5

Simple Difference symbol is used to compute the difference between two date column in the PostgreSQL table and its is multiplied by  8.64e+10 to convert results into microseconds as shown below.


select *,round(abs(birthdaytime :: date - examdatetime :: date)*(8.64e+10),2) as microseconds_diff from student_detail2;

We have typecasted the two date columns to date and then found the difference and converted the difference in microseconds.

Difference-between-two-datetime-timestamp-in-milliseconds-and-microseconds-PostgreSQL-6

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.