Get Milliseconds from Timestamp(datetime) in Postgresql

In order to get milliseconds from Timestamp(datetime) in postgresql we use Extract() function. Within the Extract() function we have to mention MILLISECONDS as we are getting MILLISECONDS from timestamp. Additionally we use DATE_PART() function to extract Milliseconds from datetime i.e. timestamp in PostgreSQL.

Let’s see how to

  • Extract milliseconds from Timestamp in Postgresql
  • Create the column which extracts milliseconds from timestamp column

With an example for both

 

Extract milliseconds from Timestamp(datetime) in Postgresql:

Within the Extract keyword we have to mention MILLISECONDS as we are getting milliseconds from timestamp

SELECT EXTRACT (MILLISECONDS FROM TIMESTAMP '2011-11-22 13:30:15.203')

So the resultant MILLISECONDS will be

Get Millisecond from timestamp in postgresql in 2

 

 

Syntax for DATE_PART() Function in Postresql:

DATE_PART(unit, source)

unit: Specifies the part of the date or timestamp to extract (e.g., ‘year’, ‘month’, ‘day’, ‘hour’, ‘minute’, ‘second’, etc.).

source: The date or timestamp from which to extract the specified part.

In Our case. The values of the unit must be “milliseconds”.

 

Extract Milliseconds from datetime (timestamp) in postgresql Simple Example:

In order to Extract milliseconds from datetime (timestamp) in postgresql we will be using DATE_PART() function.

In the below Example we will be passing milliseconds as “unit” argument and datetime as “source” argument in order to extract milliseconds from Date or datetime

SELECT DATE_PART('milliseconds', '2024-02-24 15:30:45.123456'::timestamp) AS milliseconds_part;

Output:

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-1

 

 

Extract Milliseconds from Current date in postgresql :

This query uses the CURRENT_TIMESTAMP function to get the current datetime and then extracts the milliseconds using DATE_PART()

SELECT CURRENT_TIMESTAMP AS current_datetime, DATE_PART('milliseconds', CURRENT_TIMESTAMP) AS milliseconds;

Output:

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-2

 

 

 

 

 

Extract milliseconds from date (timestamp) in postgresql table:

We will be using  Below Student_detail table for our example to depict on how to extract milliseconds from date in postgresql

Student_detail:

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-3

 

In the above table we will be using  DATE_PART() Function, which will take “milliseconds” and column named “birthdaytime” as argument. Which will extract milliseconds from “birthdaytime” column and store in the new column of postgresql table as shown below

SELECT CURRENT_TIMESTAMP AS current_datetime, DATE_PART('milliseconds', CURRENT_TIMESTAMP) AS milliseconds;

so the resultant table will have milliseconds column

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-4

 

 

EXTRACT milliseconds from Timestamp in column of Postgresql table:

We use table student_detail

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-3

Create the column which extracts MILLISECONDS from timestamp column in Postgresql:

We have created a column and stored milliseconds from Birthdaytime column

SELECT *,EXTRACT (MILLISECONDS FROM Birthdaytime) AS MILLISECONDS FROM Student_detail

So the resultant table will be

Extract-Milliseconds-from-datetime-timestamp-in-postgresql-4

 

                                                                                                       

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.