Extract Century from date or timestamp in Postgresql

To extract the century from a date or Timestamp in PostgreSQL, you can use the DATE_PART() function with the ‘century’ unit. One can also use EXTRACT() function

 

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

 

 

 

Extract Century from Date in postgresql Simple Example:

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

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

SELECT DATE_PART('century', '2024-02-24'::timestamp) AS century_part;

Output:

Extract-Century-from-date-in-PostgreSQL-1

 

Extract Century from Current date/ timestamp in postgresql :

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

SELECT CURRENT_DATE AS current_date, DATE_PART('century', CURRENT_DATE) AS century_part;

Output:

Extract-Century-from-date-in-PostgreSQL-2

 

 

 

Extract Century part from Date or timestamp in postgresql table:

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

Student_detail:

Extract-Century-from-date-in-PostgreSQL-5

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

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

so the resultant table will have century_part column

Extract-Century-from-date-in-PostgreSQL-4

 

 

Extract Century from Date in PostgreSQL table using Extract():

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

Student_detail:

Extract-Century-from-date-in-PostgreSQL-5

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


Select *, extract(CENTURY FROM birthdaytime) as century_part from student_detail

So the resultant table will have century_part column

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