SQL YEAR Function is one of the Date and Time Function, which will return an integer that represent the Year part of a specified Date.
In this article we will show you, How to use the SQL Server Year function with practical example. For this example, We are going to use the below shown data
SQL YEAR Syntax
The basic syntax of the YEAR Function in SQL Server is as follows:
date: It can be an expression that returns the date, or you can use the date and time directly.
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 Example 2
We can achieve or get the Year number using the Datepart function as well. In this 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]
From the above screenshot you can see that, the DATEPART function, and Year function are returning the same result. Note, before 2012 people used the DATEPART function to extract the Month Number but in SQL Server 2012, Microsoft introduced the Year function.
Thank You for Visiting Our Blog