PostgreSQL DATE_PART Function
Last Updated :
08 Nov, 2024
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
- field: An identifier specifying which subfield to extract from the source.
- source: The date or time value from which to extract the subfield.
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:
- century: Extract the century (e.g., 20th century = 20).
- decade: Extract the decade (e.g., 1980s = 198).
- year: Extract the year.
- month: Extract the month (1–12).
- day: Extract the day of the month (1–31).
- hour: Extract the hour (0–23).
- minute: Extract the minute (0–59).
- second: Extract the second (0–59).
- microseconds: Extract the microsecond.
- milliseconds: Extract the millisecond.
- dow: Extract the day of the week (0–6, with Sunday as 0).
- doy: Extract the day of the year (1–366).
- epoch: Extract the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC).
- isodow: Extract the ISO day of the week (1–7, with Monday as 1).
- isoyear: Extract the ISO year.
- timezone: Extract the time zone.
- timezone_hour: Extract the hour part of the time zone.
- timezone_minute: Extract the minute part of the time zone.
Return Value of DATE_PART()
- The DATE_PART() function returns a double precision type value.
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
- Precision of Return Value: The DATE_PART function returns a value of type double precision useful for calculations that may involve fractional seconds or other subfields that require precise representation.
- ISO 8601 Support: The fields
isodow
(ISO day of the week) and isoyear
(ISO year) allow us to extract date parts based on the ISO 8601 standard, crucial for international applications or standards compliance.
- Working with Epoch: Using the
epoch
field, DATE_PART can extract the number of seconds since 1970-01-01 00:00:00 UTC (the Unix epoch), facilitating easy conversion between date formats and Unix timestamps.
- Time Zone Extraction: The DATE_PART function can extract time zone information using fields like '
timezone'
, 'timezone_hour'
, and 'timezone_minute'
.
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.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read