SQL Server YEAR() Function
The YEAR()
function in SQL Server extracts the year from a date or datetime value. It's a very useful function for working with dates and performing year-based analysis or filtering.
YEAR(): Definition and Usage
YEAR()
takes a date or datetime value as input and returns an integer representing the year. This is helpful for various data manipulation and reporting tasks that involve working with yearly data.
Syntax
Syntax
YEAR(date)
Parameter Values
Parameter | Description |
---|---|
date |
The date or datetime value from which to extract the year. This is required. |
Examples
Extracting the Year from a Date
This example extracts the year (2017) from the date '2017/08/25'.
Syntax
SELECT YEAR('2017/08/25') AS Year;
Output
Year
----
2017
Extracting the Year from a DateTime
This example demonstrates that YEAR()
works correctly with datetime values; it ignores the time part and returns only the year.
Syntax
SELECT YEAR('1998/05/25 09:08') AS Year;
Output
Year
----
1998