SQL Server YEAR Function


By:

The YEAR function returns the year as an integer from the date or datetime provided.

Syntax

YEAR(date)

Parameters

  • Date – Is the date or datetime provided

Simple YEAR Example

The following example will show the year for the data March 9, 2021.

SELECT YEAR('3-9-2021 5:00:55 PM') as YEAR
YEAR FUNCTION example

YEAR function with NULL values

If the parameter of the YEAR function is NULL, a NULL value will be returned.

SELECT YEAR(NULL) as YEAR
YEAR FUNCTION with null values

Conversion failed error for the YEAR function

A typical error will occur if the date is invalid. The following example use a year of (10000). The maximum year value is 9999 (4 digits).

SELECT YEAR('13-02-10000 5:00:55 PM') as YEAR

The error message displayed will be the following.

Conversion failed when converting date and/or time from character string.

Time with the YEAR function

If the YEAR function receives just a time value, the value returned is 1900.

SELECT YEAR('5:00:55 PM') as YEAR
YEAR FUNCTION with time

YEAR function using Data from a Table

The following example will show the YEAR for the EndDate column.

SELECT YEAR(EndDate) as YEAR
FROM [Production].[ProductCostHistory]
YEAR FUNCTION in a table

Related Articles






Comments For This Article

















get free sql tips
agree to terms