SQL YEAR Function

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 YEAR method is

YEAR(date)

For this example, we use the below-shown data

Employee Table 1

SQL Server YEAR Function Example

In this example, we will show you the possible ways to use this Year

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]
Simple EXAMPLE 2

Example 2

We can achieve or get the Year number using the datepart as well. In this DateTime example, we are going to 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 are returning the same result. Note, before 2012, people used the DATEPART to extract the Year, but in 2012, Microsoft introduced the Year.

SQL YEAR Function 3

Comments are closed.