Get Minute from Timestamp(datetime) in Postgresql

In order to get minute from Timestamp in PostgreSQL we use Extract() function. Within the Extract() we have to mention MINUTE as we are getting MINUTE from timestamp. In addition to Extract() function we also use DATE_PART() function To extract minute from datetime i.e. timestamp in PostgreSQL, DATE_PART() function with the ‘minute’ unit accomplishes this task.

Let’s see how to

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

With an example for both

 

Extract minute from Timestamp in Postgresql:

Within the Extract keyword we have to mention MINUTE as we are getting minute from timestamp

SELECT EXTRACT (MINUTE FROM TIMESTAMP '2011-11-22 13:30:15')

So the resultant MINUTE will be

Get Minute from timestamp in Postgresql 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 “minute”.

 

 

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

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

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

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

Output:

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

 

 

Extract minute from Current date in postgresql :

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

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

Output:

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

 

 

Extract minute from Date in postgresql table:

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

Student_detail:

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

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

select *, DATE_PART('minute', birthdaytime::timestamp) AS minute from Student_detail

so the resultant table will have minute column

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

 

 

Extract Minute from Date in postgresql table using Extract():

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

Student_detail:

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

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

Select *, extract(MINUTE FROM birthdaytime) as Minute from student_detail

So the resultant table will have Minute column

Extract-minute-from-datetime-timestamp-in-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.