Extract Decade from date or timestamp in Postgresql

In order to Extract Decade from timestamp in postgresql we will use EXTRACT() function. In addition to extract() function there is another method to Get Decade from date or timestamp in postgresql table using DATE_PART() function. DATE_PART() function with the ‘decade’ unit accomplishes this task.

 

Extract Decade from Date or Timestamp in Postgresql:

Within the Extract() function we have to mention DECADE to get decade from timestamp

Select Extract(DECADE FROM '2024-02-24'::timestamp) as decade_part;

So the resultant decade part will be

Extract-Decade-from-date-in-postgresql-1

 

 

Syntax for DATE_PART() Function in PostgreSQL:


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 “decade”.

 

Extract Decade from Date in postgresql Simple Example:

In order to Extract decade from date in postgresql we will be using  DATE_PART() function.

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


SELECT DATE_PART('decade', '2024-02-24'::timestamp) AS decade_part;

Output:

Extract-Decade-from-date-in-postgresql-1

 

 

Extract Decade from Current date in postgresql :

This query uses the CURRENT_DATE function to get the current date and then extracts the decade from current date using DATE_PART()


SELECT CURRENT_DATE AS current_date, DATE_PART('decade', CURRENT_DATE) AS decade_part;

Output:

Extract-Decade-from-date-in-postgresql-2

 

 

 

Extract Decade part from Date or timestamp column in postgresql table – DATE_PART():

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

Student_detail:

Extract-Decade-from-date-in-postgresql-3

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


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

so the resultant table will have decade_part column

Extract-Decade-from-date-in-postgresql-6

 

 

 

Extract Decade from Date column in postgresql table using Extract():

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

Student_detail:

Extract-Decade-from-date-in-postgresql-3

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


Select *, extract(DECADE FROM birthdaytime) as decade_part from student_detail

So the resultant table will have decade_part column

Extract-Decade-from-date-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.