MySQL Year Function

MySQL Year is one of the Date Functions, which returns the Year from the given date. This Year function returns values ranging from 1000 to 9999, and the syntax of it is as shown below:

YEAR(date or expression);

MySQL Year function Example

The below-shown queries help you understand the basic use of the Year function. Here, we are returning the Year value from the date expression and the DateTime expression.

TIP: This Date method returns 0 if the date argument is 0000-00-00 or, say, zero datepart.

SELECT YEAR('2016-11-25');

SELECT YEAR('2018-10-22 01:09:22');
Example 2

Let us see another example of this method, and here, we are extracting Year from the current datetime returned by the Now().

SELECT YEAR(NOW());
Example 3

The following query show what happens when we try to extract the year from a date or datetime in string format.

SELECT YEAR('2016-11-44');

SELECT YEAR(NOW() + 5);

SELECT YEAR(0);
Year Example 4

From the above screenshot, (‘2016-11-44’); is returning NULL because it is an invalid one. The last statement (0) means we are trying to extract Year from a zero datepart. That’s why MySQL returns NULL.