SQL Tutorial SQL Advanced SQL Database SQL References

MySQL YEAR() Function



The MySQL YEAR() function returns the year portion of a given date or datetime value. It returns a four-digit year and it can be between 1000 to 9999.

Syntax

YEAR(datetime)

Parameters

datetime Required. Specify a date or datetime value from which to extract the year.

Return Value

Returns the year part of a given date or datetime value.

Example 1:

The example below shows the usage of YEAR() function.

mysql> SELECT YEAR('2018-08-18');
Result: 2018

mysql> SELECT YEAR('2018-08-18 10:38:42');
Result: 2018

mysql> SELECT YEAR('2018-08-18 10:38:42.000004');
Result: 2018

mysql> SELECT YEAR('2014-10-25');
Result: 2014

mysql> SELECT YEAR(CURDATE());
Result: 2021

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-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:

OrderQuantityPriceOrderTimeYEAR_Value
1001.582017-08-18 10:38:422017
1201.612018-03-23 07:14:162018
1251.782018-09-12 05:25:562018
501.802019-01-16 11:52:052019
2001.722020-02-06 09:31:342020

❮ MySQL Functions