Back to Library

YEAR() in SQL

Extracts the year value from a date or timestamp expression.

Syntax

YEAR(date_expression)
 -- Some databases use: EXTRACT(YEAR FROM date_expression)
Return type
INTEGER

YEAR() Function Example

SELECT YEAR(order_date) FROM orders; 
-- Output: 2024 (year extracted from date)

What is YEAR() in SQL?

The YEAR() function in SQL extracts the year value from a given date or timestamp expression. It is commonly used for date-based filtering, reporting, and grouping data by year.

This function is supported in multiple databases:

  • MySQL, PostgreSQL, and SQL Server: Use YEAR(date_column).

  • Oracle: Uses EXTRACT(YEAR FROM date_column).

  • SQLite: Uses strftime('%Y', date_column).

Parameters:

  • date_expression: Date, datetime, or timestamp value

Example Use Cases:

-- Group sales by year

SELECT YEAR(sale_date) AS year, SUM(amount) FROM sales GROUP BY YEAR(sale_date); 
-- Output:
year    sum_amount
----    ----------
2021    1245678.90
2022    1567890.45
2023    1789234.56
2024    1356789.23

-- Find records from specific year 

SELECT * FROM transactions WHERE YEAR(transaction_date) = 2024; 
-- Output:
transaction_id    transaction_date    amount    customer_id    status
--------------    ----------------    ------    -----------    -------
T001              2024-01-15         599.99    C123          Completed
T002              2024-02-28         299.50    C456          Completed
T003              2024-03-10         749.99    C789          Completed
T004              2024-04-05         199.99    C234          Completed
T005              2024-05-20         449.99    C567          Completed

-- Calculate years of service 

SELECT employee_name, YEAR(CURRENT_DATE) - YEAR(hire_date) AS years_employed FROM employees;
-- Output:
employee_name    years_employed
-------------    --------------
John Smith       5
Maria Garcia     3
Robert Johnson   7
Sarah Williams   2
James Brown      4

Notes:

  • Behavior: Returns NULL if input is NULL

  • Performance Considerations: May prevent index usage if not used carefully

  • Version Info: Core function available in most DBMS

  • Deprecated/Recommended Alternatives: Some DBMS prefer EXTRACT()

Error Handling:

  • Error: Returns NULL for invalid dates

  • Recommendation: Validate date format before extraction

Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

YEAR()

YEAR(date)

Returns NULL

PostgreSQL

EXTRACT(YEAR FROM)

EXTRACT(YEAR FROM date)

Returns NULL

SQL Server

YEAR()

YEAR(date)

Returns NULL

SQLite

strftime('%Y')

strftime('%Y', date)

Returns NULL

BigQuery

EXTRACT(YEAR FROM)

EXTRACT(YEAR FROM date)

Returns NULL

Snowflake

YEAR()

YEAR(date)

Returns NULL

Athena

EXTRACT(YEAR FROM)

EXTRACT(YEAR FROM date)

Returns NULL

Related Functions:

DATEFROMPARTS()

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

DATEFORMAT()

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

EOMONTH()

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

YEAR()

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

DATEDIFF()

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

DATEADD()

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

GETDATE()

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

One place for all your queries,
directly on your SQL editor