How To Get Year From Date in SQL Server
YEAR() function is used to get year from the date provided in the YEAR() function. It gives the single year result in positive integer.
General Syntax is as follows:
Get year from GETDATE() Function
We can get year from GETDATE() function which gives the year in integer from the current date.
SELECT YEAR(GETDATE()) AS YearFromGetDate
Get month from date provided as string in single quotes
We can get year from date provided as string in single quotes which gives the year in integer.
SELECT YEAR('2022/01/31') AS YearFromDate
Get month from table Column
We can get year from table having DATETIME data type. To get the year from the table column, first we create a table and insert some rows and then we will get the year from that particular column.
USE HR GO CREATE TABLE tblEmployee ( EmpID INT PRIMARY KEY NOT NULL, EmpName VARCHAR(20), EmpGender VARCHAR(10), EmpAge INT, EmpSalary INT, EmpJoiningDate DATETIME ) GO
Let’s insert some records into tblEmployee table.
USE HR GO INSERT INTO tblEmployee VALUES (1,'Simon','Male',25,25000,'2015-01-01 22:04:49.400'), (2,'Dave','Male',29,15000,'2016-03-25 20:04:55.400'), (3,'Sara','Female',35,20000,'2017-05-01 18:04:35.400'), (4,'Julia','Female',36,35000,'2018-01-10 15:03:45.400'), (5,'Sam','Male',32,35000,'2020-03-20 10:02:25.400') GO
Below is the result of records of table tblEmployee.
Now, below is the query to get year from the EmpJoiningDate column of the table tblEmployee.
USE HR GO SELECT EmpName, EmpGender, EmpSalary, YEAR(EmpJoiningDate) AS JoiningYear FROM tblEmployee GO