SQL YEAR

The SQL YEAR function is used to extract the year from a given date or datetime value. This function is commonly used in date calculations, filtering data by year, and performing aggregations based on years.

In this tutorial, we will explore the SQL YEAR function, its syntax, and practical examples to demonstrate its usage.


Syntax of SQL YEAR Function

The basic syntax of the YEAR function in SQL is:

</>
Copy
SELECT YEAR(date_column) AS extracted_year
FROM table_name;

You can also use YEAR with a direct date value:

</>
Copy
SELECT YEAR('2025-07-15') AS extracted_year;

This will return 2025 as the extracted year.


Step-by-Step Examples Using SQL YEAR Function

1 Extracting Year from a Date Column

Let’s create a employees table and use the YEAR function to extract the year from the joining date of employees.

</>
Copy
CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    joining_date DATE
);

Insert sample data:

</>
Copy
INSERT INTO employees (name, joining_date)
VALUES 
('Arjun', '2018-05-23'),
('Ram', '2020-08-17'),
('Priya', '2022-11-09');

Now, let’s extract the year from the joining_date column:

</>
Copy
SELECT name, joining_date, YEAR(joining_date) AS joining_year
FROM employees;