DATE_PART()

SQL · Date Functions

Theory

DATE_PART is the function-style version of EXTRACT. Instead of the keyword form EXTRACT(MONTH FROM order_date), you write DATE_PART('month', order_date), passing the field name as a text string. Both return the same number, so DATE_PART('month', '2026-11-05') gives 11.

DATE_PART comes from PostgreSQL and is also available in Snowflake and Redshift. SQL Server has a similar function called DATEPART(month, order_date), with no underscore and no quotes around the unit. MySQL and SQLite do not have DATE_PART; MySQL uses EXTRACT or MONTH(), and SQLite uses strftime('%m', ...). A very common report pattern is to group by the extracted part, for example counting orders per month to see seasonality. Remember that a month number alone mixes years together, so group by year and month both when your data spans more than one year.

Why we use it

It does the same job as EXTRACT but in a normal function shape, which some people find easier to read and to build dynamically (the unit is just a string). It is the everyday choice in PostgreSQL-style code.

When to use it

Use DATE_PART in PostgreSQL, Snowflake or Redshift when you need a numeric part of a date. Use EXTRACT for code that should move between databases, and DATE_TRUNC when you want a real date (first day of the month) instead of a number.

How it works

  1. Call DATE_PART('month', order_date)
  2. Read the unit string ('year', 'month', 'dow'...)
  3. Decode order_date and pick that part
  4. Return a number such as 11
  5. Data spans several years? Yes: Group by year AND month together. No: Group by month alone.
  6. Counts or sums per period

Syntax

DATE_PART('field', date)

Example

SELECT DATE_PART('month', hire_date) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic