YEAR() Function in SQL Server
Last Updated :
27 Sep, 2024
The YEAR()
function in SQL Server is a powerful tool designed to extract the year component from a given date or datetime expression. It allows users to isolate the year as an integer value and facilitating various date-related operations and analyses.
In this article, We will learn about the YEAR() Function in SQL Server along with their syntax and so on.
YEAR() Function in SQL Server
- The
YEAR()
function in SQL Server is used to extract the year part from a date or datetime expression.
- It returns the year as an integer.
Features of YEAR() Function in SQL Server
- This function is used to find the year for a date specified.
- This function comes under Date Functions.
- This function accepts only one parameter i.e, date.
- This function can also include time with the stated date.
Syntax:
YEAR(date)
Parameter:
This method accepts only one parameter as given below :
- date : Specified date from which the year is to be returned.
Returns:
It returns the year for a date specified.
Example of YEAR() Function in SQL Server
Example 1:
Using YEAR() function and getting the year from the date specified.
SELECT YEAR('2020/01/02');
Output:
2020
Example 2:
Using YEAR() function with a variable and getting the year from the date specified.
DECLARE @date VARCHAR(50);
SET @date = '2017/07/05';
SELECT YEAR(@date);
Output:
2017
Example 3:
Using YEAR() function with date as parameter which includes time as well.
SELECT YEAR('2018/11/22 07:44');
Output:
2018
Example 4:
Using YEAR() function with a variable and a date as parameter which includes time as well.
DECLARE @date VARCHAR(50);
SET @date = '1995/05/13 23:59';
SELECT YEAR(@date);
Output:
1995
Conclusion
In summary, the YEAR()
function simplifies the process of retrieving the year from date values in SQL Server. By handling various date formats and accommodating both date and time components, this function proves to be invaluable for developers and data analysts.