SQL Server YEAR function is one of the Date and Time methods, which will return an integer representing the Year part of a specified date. The syntax of the SQL YEAR function is
YEAR(date)
For this example, we use the below-shown data
SQL Server YEAR Function Example
In this example, we will show you the possible ways to use this Year method.
SELECT YEAR('07-19-1975') AS [Year Example 1] SELECT YEAR('2015-11-24 12:29:44.513') AS [Year Example 2] SELECT YEAR(GETDATE()) AS [This Year]
Year Example 2
We can achieve or get the Year number using the datepart as well. In this DateTime example, we will return the Year numbers from Hire Date in the Employee table using the DATEPART and the Year function.
SELECT [EmpID] ,[FirstName] + ' '+ [LastName] AS [Full Name] ,[Occupation] ,[YearlyIncome] ,[HireDate] ,YEAR([HireDate]) AS [Year Number] ,DATEPART(year, [HireDate]) AS [This Year From DatePart] FROM [Employee]
As you can see, both Server methods return the same result. Note, before 2012, people used the DATEPART to extract the Year, but in 2012, Microsoft introduced the Year.
Comments are closed.