MySQL YEAR() Function

MySQL YEAR() function; Through this tutorial, i am going to show you MySQL YEAR() function with the help of examples.

MySQL YEAR() Function

YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999.

Syntax of MySQL YEAR() Function

The YEAR() function syntax is:

 YEAR(argument) 

Here, The argument is the date you want the YEAR to return.

Example 1 – MySQL YEAR() Function

Let’s take an example using mysql year() function; as follows:

SELECT YEAR('2020-06-18') AS 'Result';

Output-1

+--------+
| Result |
+--------+
|   2020 |
+--------+

Example 2 – MySQL YEAR() Function

Let’s take second example using the mysql year() function; as follows:

SELECT YEAR('2022-10-18') AS 'Result';

Output-2

+--------+
| Result |
+--------+
|  2022  |
+--------+

Example 3 – MySQL YEAR() Function

To get the current year data from the MySQL database with the help of year() function; as follows:

 SELECT name, YEAR(CURDATE())
 FROM employees 
 WHERE YEAR(created_at) = YEAR(NOW()) ORDER BY id DESC

Example 4 – MySQL YEAR() with NOW() Function

To extract part of the YEAR part from the current date and time (which is now returned using the () function); as follows:

  SELECT 
  NOW(),
  YEAR(NOW());

Output-4

+---------------------+--------------------+
| NOW()               | YEAR(NOW())        |
+---------------------+--------------------+
| 2019-07-10 18:30:44 |         2019       |
+---------------------+--------------------+

Example 5 – Fetch Current Year Data Into MySQL Database

Use the following query to get year data from the mysql database table with the help of year() function; as follows:

SELECT COUNT(id) as Count,YEAR(created_at) as Year
FROM employees
GROUP BY YEAR(created_at)

Conclusion

MySQL year() function tutorial, you have learned how to use MySQL YEAR() function with various examples.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *