EXTRACT()

SQL · Date Functions

Theory

EXTRACT pulls one field out of a date or timestamp and returns it as a number. EXTRACT(YEAR FROM hire_date) on 2023-07-01 gives 2023, EXTRACT(MONTH FROM ...) gives 7 and EXTRACT(DAY FROM ...) gives 1. Common fields are YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE, SECOND, and in many databases DOW (day of week) and WEEK.

EXTRACT is standard SQL and works in PostgreSQL, MySQL, Oracle, Snowflake and BigQuery. SQL Server instead uses DATEPART(year, hire_date) or the shortcuts YEAR(), MONTH() and DAY(). SQLite has no EXTRACT; you use strftime('%Y', hire_date), which returns text, so wrap it in CAST(... AS INTEGER) when you need a number. Typical uses are grouping sales by year or month, finding everyone hired in a given year, or spotting weekend orders.

Why we use it

Dates are stored as one value, but questions are often about a part of it: 'how many people joined each year?' or 'which month sells most?'. EXTRACT breaks the date apart so you can group, filter or compare on that part.

When to use it

Use it to group or label by year, month or weekday. For filtering on a big table, a date range (hire_date >= '2024-01-01' AND hire_date < '2025-01-01') is faster than EXTRACT(YEAR ...) = 2024, because it can use an index.

How it works

  1. Read a date value, e.g. 2023-07-01
  2. Choose the field: YEAR, MONTH, DAY, HOUR...
  3. Database decodes the date into its parts
  4. Return the chosen part as a number (2023, 7, 1)
  5. Used in GROUP BY or WHERE? Yes: Group or filter by that part. No: Show it as a column.
  6. Date part ready for analysis

Syntax

EXTRACT(field FROM date)

Example

SELECT EXTRACT(YEAR FROM 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