You are currently viewing How To Get Year From Date in SQL Server

How To Get Year From Date in SQL Server

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:

SELECT YEAR(DateParameter)

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

Output

Get year from getdate function

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

Output

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.

Day function table data

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

Output

Get year from tblEmployee table

Recommended Readings

Buy Best Stationery Products in Pakistan

Piano Ballpoint Pen

Piano Ballpoint Pen

ORO Trimetal Pencil

ORO Trimetal

Dollar Clipper Pen

UHU 10×7 ml Pack of 10 PCs

UHU 10×7 ml Pack of 10 PCs

Glue Stick 8g Pack Of 12 PCs

Glue Stick 8g

Color Pencils Box of 12 PCs

Deer Multi Color Pencils