EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL DATE_PART()
 

PostgreSQL DATE_PART()

Priya Pedamkar
Article byPriya Pedamkar

Updated May 29, 2023

PostgreSQL DATE_PART()

 

 

Introduction to PostgreSQL DATE_PART()

PostgreSQL DATE_PART() function is mainly used to return the part of the date and time; the date_part function in PostgreSQL will subtract the subfield from the date and time value. PostgreSQL date_part function will allow retrieving subfields from the date and time value, e.g., week, month, and year. Basically, there are two parameters we have a pass with the date_part function, i.e., field and source; field determines which field we have extracted from the source.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Given below is the syntax of the PostgreSQL date_part function:

date_part (text, timestamp)
date_part (text, interval)
date_part (unit, date)
date_part (field, source)

Given below is the parameter description of the above syntax: 

  • Date part: We use the date_part function in PostgreSQL to extract the date part values from an expression. We can provide an argument with the date_part function as text and timestamp in PostgreSQL.
  • Text: The first parameter used with the date_part function in PostgreSQL is “text”.
  • Timestamp: This is the second parameter of the date_part function in PostgreSQL. We have used to define the timestamp value with the date_part function in PostgreSQL.
  • Interval: In PostgreSQL, we use the interval value the same as a timestamp value. We can use an interval instead of a timestamp value in PostgreSQL.
  • Unit: Unit type in date_part function is interval such as minute, hour, and month. Unit is an essential parameter in the date_part function.
  • Date: We use a date, timestamp, time, or interval value to perform partial execution.
  • Field: The date_part function in PostgreSQL uses a parameter to specify which field to extract from the source.
  • Source: In the date_part function, we define the second parameter as the source. This parameter specifies the date or timestamp to extract the specified field. This is the same as the interval parameter in the date_part function in PostgreSQL.

How PostgreSQL DATE_PART() Function works?

If we want to return only the date and time value in PostgreSQL same time, we are using the date_part function. Using the date_part function in PostgreSQL, we can extract the century from the time stamp. We must pass the field argument in the date_part function to extract the year from the date timestamp.

In PostgreSQL, the unit type has the following form used in the date_part function. The unit type is a day, month, minute, and hour.

  • Century: It uses the Gregorian calendar in which the first century will start from the ‘0001-01-01 00:00:00 AD’.
  • Day: Defined as the day of the month from 1 to 31.
  • Decade: In the date_part function of PostgreSQL, we define a decade as the year divided by 10.
  • Day of week: Defined as a day of the week from Sunday to Saturday.
  • Day of year: We define this as the day of the year, ranging from January to December.
  • Epoch: This is defined as several interval values in the date_part function.
  • Hour: Defined as an hour of the day from 0 to 23.
  • Millennium: In the date_part function, we define the millennium value.
  • Milliseconds: This is defined as a second multiply with 1000.
  • Microseconds: This is defined as a second multiply with 100000.
  • Minute: This is defined as a minute of the hour from 0 to 59.
  • Quarter: Quarter is defined in date_part function is 1 to 4.
  • Second: This is a fractional second in the date_part function.
  • Year: This is defined as a year in four-digit value.

To extract the second, minute, and hour from the timestamp using the date_part function, we need to pass the corresponding value that is second, minute, and hour.

Examples of PostgreSQL DATE_PART()

Given below are the examples mentioned:

Example #1

Extract the century from a timestamp using the date_part function.

In the example below, we used the years 2020 and 1920 to extract a century from the timestamp.

Code:

SELECT date_part('century', TIMESTAMP '2020-05-19');
SELECT date_part('century', TIMESTAMP '1920-05-19');

Output:

PostgreSQL DATE_PART() 1

In the above example, the year 1920 will display a century as 20, and the year 2020 will display the century as 21.

Example #2

Extract the year from a timestamp using the date_part function.

In the below example, we have used the year 2020 to extract the year from the timestamp.

Code:

SELECT date_part('year', TIMESTAMP '2020-05-19');

Output:

Extract year

Example #3

Extract a quarter from a timestamp using the date_part function.

In the below example, we have used the year 2020 to extract a quarter from the timestamp.

Code:

SELECT date_part('quarter', TIMESTAMP '2020-05-19');

Output:

Extract quarter from timestamp

Example #4

Extract a month from a timestamp using the date_part function.

In the below example, we have used a month as May to extract the month from the timestamp.

Code:

SELECT date_part('month', TIMESTAMP '2010-05-19');

Output:

Extract month from timestamp

Example #5

Extract a week from a timestamp using the date_part function.

In the below example, we have used the year 2010 and the month of May to extract the week from the timestamp.

Code:

SELECT date_part(week, TIMESTAMP '2010-05-19');

Output:

Extract week from timestamp

Example #6

Extract the day from a timestamp using the date_part function.

In the below example, we have used the year 2020 and the month of May to extract the day from the timestamp.

Code:

SELECT date_part('day',TIMESTAMP '2020-05-19 00:00:00');

Output:

PostgreSQL DATE_PART() 6JPG

Example #7

Extract hour, minute, and second from a timestamp using the date_part function.

The below example shows that Extract hour, minute, and second from a timestamp using the date_part function.

Code:

SELECT date_part('hour', TIMESTAMP '2020-05-19 12:30:40') h,  date_part('minute', TIMESTAMP '2020-05-19 12:30:40') m, date_part('second',TIMESTAMP '2020-05-19 12:30:40') s;

Output:

PostgreSQL DATE_PART() 7 JPG

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL DATE_PART()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL GRANT
  2. Trunc() PostgreSQL
  3. PostgreSQL Auto Increment
  4. PostgreSQL Primary Key

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW