MySQL Year function is one of the Date Function, which is used to return the Year from the given date. This function returns value range from 1000 to 9999.
In this article, we will show you, How to use this MySQL Year function to get the Year from an expression with example.
MySQL Year Syntax
The basic syntax of the Year() Function in MySQL is as shown below:
YEAR(date or expression);
TIP: This MySQL Year function returns 0, if the date argument is 0000-00-00 or say, zero date part.
MySQL Year function Example
The below shown queries helps you understand the basic use of Year function. Here, we are returning the Year value from the date expression, and the Date and time expression.
SELECT YEAR('2018-10-22 01:09:22');
Let us see another example of MySQL Year function. Here, we are extracting Year from the current date and time returned by the Now() function.
MySQL Year Function Example 2
The following query will show you, what happens when we try to extract the year from a date or date and time in string format.
SELECT YEAR(NOW() + 5);
From the above screenshot, YEAR(‘2016-11-44’); is returning NULL. Because, it is an invalid date.
Last statement YEAR(0) means, we are trying to extract Year from a zero date part. That’s why, it returns NULL.
Thank You for Visiting Our Blog