Home >Database >Mysql Tutorial >How to query the current year in mysql
In mysql, you can use the YEAR() and NOW() functions to query the current year. The YEAR() function can obtain the year value from the specified date value, and the NOW() function can obtain the current date and time; the YEAR() and NOW() functions can obtain the year value from the current date and time. Syntax "SELECT YEAR(NOW());".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
In mysql, the YEAR() function can get the year value from the specified date value.
YEAR() function accepts the date parameter and returns the year of the date. Please refer to the syntax of the YEAR() function:
YEAR(date);
The YEAR() function returns a year value for a specified date, ranging from 1000 to 9999. If the date is zero, the YEAR() function returns 0.
Example: Return the year of 2018-01-01, which is 2018.
SELECT YEAR('2018-01-01');
But how to get the current year without knowing the exact date?
Implementation method: YEAR() function can query the current year with NOW() function.
Implementation idea:
NOW() function can get the current date and time
Pass the obtained current date Enter the YEAR() function as a parameter to return the year of the date.
SELECT YEAR(NOW());
YEAR() function returns the year information of the current date and time provided by the NOW() function.
Extended knowledge:
If the date is NULL, the YEAR() function will return NULL, as shown in the following example:
SELECT YEAR(NULL);
As mentioned before, the YEAR() result of zero date is NULL (the evaluation result of some MySQL versions is: 0):
SELECT YEAR('0000-00-00');
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to query the current year in mysql. For more information, please follow other related articles on the PHP Chinese website!