Articles Cookbook
Back to list
SQL Server

How to Get the Year from a Date in T-SQL

Database:

Operators:

YEAR()

Problem:

You’d like to get the year from a date field in a SQL Server database.

Example:

Our database has a table named Children with data in the columns Id, FirstName, LastName, and BirthDate.

IdFirstNameLastNameBirthDate
1JaneSmith2018-06-20
2GaryBrown2010-02-02
3LoraAdams2014-11-05

Let’s get the year from each child’s birthdate.

Solution:

We’ll use the YEAR() function. Here’s the query you would write:

SELECT FirstName,
       LastName,
       YEAR(BirthDate) AS BirthYear
FROM Children;

Here’s the result of the query:

FirstNameLastNameBirthYear
JaneSmith2018
GaryBrown2010
LoraAdams2014

Discussion:

Use SQL Server’s YEAR() function if you want to get the year part from a date. This function takes only one argument – a date, in one of the date and time or date data types. (In our example, the column BirthDate is a date data type). The argument can be a column name or an expression. (In our example, the argument is the BirthDate column.)

The YEAR() function returns an integer. For Jane Smith, it returns the integer 2018 from the birth date '2018-06-20'.

Recommended courses:

Recommended articles:

See also:

go to top