EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL DATE_PART()

PostgreSQL DATE_PART()

By Priya PedamkarPriya Pedamkar

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more