SQL Server YEAR() Function
The SQL Server (Transact-SQL) YEAR() function returns the year for the specified date. It returns a four-digit year and it can be between 1000 to 9999.
Syntax
YEAR(date)
Parameters
date |
Required. Specify a date from which to extract the year. |
Return Value
Returns the year part of the given date value.
Example 1:
The example below shows the usage of YEAR() function.
SELECT YEAR('2018-08-18'); Result: 2018 SELECT YEAR('2018-08-18 10:38:42'); Result: 2018 SELECT YEAR('2018-08-18 10:38:42.0000004'); Result: 2018 SELECT YEAR('2014-10-25'); Result: 2014
Example 2:
Consider a database table called Orders with the following records:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 |
120 | 1.61 | 2018-03-23 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 |
200 | 1.72 | 2020-02-06 09:31:34 |
The statement given below can be used to get the year portion of records of column OrderTime:
SELECT *, YEAR(OrderTime) AS YEAR_Value FROM Orders;
This will produce the result as shown below:
OrderQuantity | Price | OrderTime | YEAR_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 | 2017 |
120 | 1.61 | 2018-03-23 07:14:16 | 2018 |
125 | 1.78 | 2018-09-12 05:25:56 | 2018 |
50 | 1.80 | 2019-01-16 11:52:05 | 2019 |
200 | 1.72 | 2020-02-06 09:31:34 | 2020 |
❮ SQL Server Functions