SQL YEAR Function

SQL YEAR function is one of the Date and Time Function, which will return an integer that represents the Year part of a specified date. The syntax of the YEAR function in SQL Server is

YEAR(date)

For this Year function example, we use the below-shown data

SQL YEAR Function 1

SQL YEAR Function Example 1

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

-- Example for SQL YEAR Function

-- Testing Year Function with Random Date 
SELECT YEAR('07-19-1975') AS [Year Example 1]

-- Testing Year Function with Random Date and Time 
SELECT YEAR('2015-11-24 12:29:44.513') AS [Year Example 2]

-- Testing Year Function with Todays Date 
SELECT YEAR(GETDATE()) AS [This Year]
SQL YEAR Function 2

YEAR Function Example 2

We can achieve or get the Year number using the datepart function as well. In this Date and Time Function example, we are going to return the Year numbers from Hire Date in Employee table using the DATEPART Function, and the Year function.

-- Example for SQL YEAR Function
USE [SQL Tutorial]
GO
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 the SQL Server functions are returning the same result. Note, before 2012 people used the DATEPART function to extract the Year but in 2012, Microsoft introduced the Year function.

SQL YEAR Function 3

Comments are closed.