Beginner

266. DATE_PART Function

Learn how to extract a specific component such as year, month, or day from a date.

📘 Lesson 266 of 728 ⏱ 9 min 🎯 Intermediate

DATE_PART extracts a specified component of a date or timestamp, such as year, month, day, hour, or minute, in databases that support the function.

Analysts frequently need individual date components for grouping, filtering, trend analysis, and reporting.

Extract the year from an order date.

SQL
SELECT
    OrderDate,
    DATE_PART('year', OrderDate) AS OrderYear
FROM Orders;

DATE_PART is useful for monthly reporting, yearly analysis, seasonal analysis, and time-based grouping.

  • DATE_PART extracts date components.
  • It can extract year, month, day, and time components.
  • It is useful for grouping.
  • Syntax varies between databases.
  • It is commonly used in analytical queries.
What does DATE_PART do?

DATE_PART extracts a selected component from a date or timestamp.

Why extract the year from a date?

Extracting the year allows records to be grouped, filtered, and compared by year.

Have you finished this lesson?

Introduction to SQL – All Lessons