In order to get Microseconds from Timestamp in postgresql we use Extract() function. Within the Extract() function we have to mention MICROSECONDS as we are getting MICROSECONDS from timestamp. Additionally we use DATE_PART() function to extract Microseconds from datetime i.e. timestamp in PostgreSQL.
Let’s see how to
- Extract Microseconds from Timestamp in Postgresql
- Create the column which extracts Microseconds from timestamp column
With an example for both
Extract Microseconds from Timestamp in Postgresql:
Within the Extract keyword we have to mention MICROSECONDS as we are getting Microseconds from timestamp
SELECT EXTRACT (MICROSECONDS FROM TIMESTAMP '2011-11-22 13:30:15.203')
So the resultant MICROSECONDS will be
Syntax for DATE_PART() Function in PostgreSQL:
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 “microseconds”.
Extract Microsecond from datetime(timestamp) in postgresql Simple Example:
In order to Extract microseconds from datetime (timestamp) in postgresql we will be using DATE_PART() function.
In the below Example we will be passing microseconds as “unit” argument and datetime as “source” argument in order to extract microseconds from Date or datetime
SELECT DATE_PART('microseconds', '2024-02-24 15:30:45.123456'::timestamp) AS microseconds_part;
Output:
Extract Microseconds from Current date in postgresql :
This query uses the CURRENT_TIMESTAMP function to get the current datetime and then extracts the microeconds using DATE_PART()
SELECT CURRENT_TIMESTAMP AS current_datetime, DATE_PART('microseconds', CURRENT_TIMESTAMP) AS microseconds;
Output:
EXTRACT Microseconds from Timestamp in column of Postgresql table – DATE_PART():
We will be using Below Student_detail table for our example to depict on how to extract microseconds from date in postgresql
Student_detail:
In the above table we will be using DATE_PART() Function, which will take “microseconds” and column named “birthdaytime” as argument. Which will extract microseconds from “birthdaytime” column and store in the new column of postgresql table as shown below
select *, DATE_PART('microseconds', birthdaytime::timestamp) AS microseconds from Student_detail
so the resultant table will have microseconds column
Extract Microseconds from Date in postgresql table using Extract():
We will be using Below Student_detail table for our example to depict on how to extract Microseconds from date in postgresql
Student_detail:
In the above table we will be using EXTRACT() Function, which will take “Microseconds” and column named “birthdaytime” as argument. Which will extract Microseconds from “birthdaytime” column and store in the new column of postgresql table as shown below
Select *, extract(MICROSECONDS FROM birthdaytime) as microseconds from student_detail
So the resultant table will have microseconds column