CodeBlanks Logo Icon CodeBlanks

EXTRACT

Pull year, month, day out of a date.

EXTRACT

With EXTRACT you pull a part like year, month or day from a date. You use YEAR, MONTH, DAY.

SELECT EXTRACT(YEAR FROM order_date) AS yil FROM orders;

This query returns the year of each order.

EXTRACT(YEAR FROM order_date) AS yil is selected from orders.

EXTRACT — details

Some databases offer a YEAR(date) function. EXTRACT is ANSI standard and works across all major databases.

Practice EXTRACT

Select the year of order_date aliased as yil from orders.

Starter code

query.sql
-- extract year
SELECT
FROM orders

Sample solution

query.sql
SELECT EXTRACT(YEAR FROM order_date) AS yil
FROM orders

Expected output

yil
2025
2026

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Extract year

Question
SELECT ____(YEAR FROM order_date) FROM orders;
Answer
SELECT EXTRACT(YEAR FROM order_date) FROM orders;

Explanation: EXTRACT is the right answer.

2 What does EXTRACT do?

  • Gets part of a date Correct answer
  • Adds to a date
  • Deletes a date
  • Formats a date

Explanation: EXTRACT pulls a part from a date.

3 EXTRACT: match the terms

Term Meaning
EXTRACT extract
YEAR year
MONTH month
DAY day

All lessons in Beginner