Home >Database >Mysql Tutorial >How to query the current year in mysql

How to query the current year in mysql

青灯夜游
青灯夜游Original
2022-11-14 19:32:3714995browse

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());".

How to query the current year in mysql

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');

How to query the current year in mysql

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());

How to query the current year in mysql

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);

How to query the current year in mysql

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');

How to query the current year in mysql

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn