A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-date_part-function/ below:

PostgreSQL DATE_PART Function - GeeksforGeeks

PostgreSQL DATE_PART Function

Last Updated : 15 Jul, 2025

Handling dates and times efficiently is essential for data-driven applications, and PostgreSQL provides powerful built-in functions for managing and manipulating time-based data. One such function is the DATE_PART() function, which allows us to extract specific subfields from date and timestamp values.

In this article, we will cover the syntax, common use cases, and practical examples of the DATE_PART() function, enabling us to work with time-related data effectively in PostgreSQL.

What is the DATE_PART() Function in PostgreSQL?

The DATE_PART() function is a PostgreSQL date and time function that extracts a specified subfield (such as year, month, day, or hour) from a given timestamp or date. It allows us to query and manipulate time-related data by isolating individual components from larger date-time values.

The DATE_PART() function works with timestamps, dates, and intervals to provide smaller information about various parts of the time. This can be particularly useful for tasks like generating reports, aggregating data by specific time units (e.g., by month or by day), and performing time-based calculations.

Syntax

DATE_PART(field, source)

Key Terms

Permitted Field Values

In the above syntax, the field is an identifier that is used to set the field to extract the data from the source. The permitted field values are mentioned below:

Return Value of DATE_PART() Examples of PostgreSQL DATE_PART Function

Let us take a look at some of the examples of DATE_PART Function to better understand the concept. Examples of PostgreSQL DATE_PART Function help illustrate its flexibility and how it can be used to extract specific components from date and time values for more detailed analysis.

Example 1: Extracting the Century from a Timestamp

In this example, we will use DATE_PART() function to extract the data regarding the century from a timestamp.

Query:

SELECT date_part('century', TIMESTAMP '2020-01-01');

Output

Example 2: Extracting Hour, Minute, and Second from a Timestamp

In this example we will extract the hour, minute, second from a time stamp type value, by passing the corresponding value hour, minute and second to the DATE_PART() function.

Query:

SELECT date_part('hour', TIMESTAMP '2020-03-18 10:20:30') h,
date_part('minute', TIMESTAMP '2020-03-18 10:20:30') m,
date_part('second', TIMESTAMP '2020-03-18 10:20:30') s;

Output

Example 3: Extracting Day of the Week and Day of the Year from a Timestamp

In this example, we will query for the day of week and day of the year from a timestamp, through the use of the following statement.

Query:

SELECT date_part('dow', TIMESTAMP '2020-03-18 10:20:30') dow,
date_part('doy', TIMESTAMP '2020-03-18 10:20:30') doy;

Output

Important Points About PostgreSQL DATE_PART Function Conclusion

The DATE_PART() function in PostgreSQL is an essential tool for extracting specific date or time components from a timestamp or date value. By using this function, we can easily manipulate and analyze time-based data, enabling more precise reporting and calculations. Whether we're extracting the day of the week, epoch time, or time zone information, DATE_PART() enhances our ability to work with temporal data in PostgreSQL.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4