YEAR

The SQL YEAR function is a built-in function in most SQL database management systems that allows you to extract the year from a date or datetime data type. The function returns an integer representing the year component of the specified date.

Syntax

The syntax of the YEAR function may vary slightly depending on the specific database management system you are using, but generally it follows this format:

YEAR(date)

Where date is the date or datetime expression from which you want to extract the year.

Example

For example, if you have a table orders with a column order_date that stores the date when each order was placed, you can use the YEAR function to extract the year of each order:

SELECT 
YEAR(order_date) AS order_year
FROM orders;

This will return a result set with a single column named order_year that contains the year component of each order_date.

It’s worth noting that the YEAR function only works with date and datetime data types. If you try to apply it to a non-date data type, you will receive an error. Additionally, the YEAR function may behave differently depending on the database management system you are using, particularly with regards to handling NULL values.

Overall, the SQL YEAR function is a useful tool for working with dates and extracting specific date components in SQL queries.