Get current year in MySQL WHERE clause?


To get current year, use YEAR() along with CURDATE(). Let us first create a table −

mysql> create table DemoTable1360
    -> (
    -> JoiningYear int
    -> )
    -> ;
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1360 values(1998);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1360 values(2018);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1360 values(2016);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1360 values(2019);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1360 values(2017);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1360 values(2015);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1360;

This will produce the following output −

+-------------+
| JoiningYear |
+-------------+
|        1998 |
|        2018 |
|        2016 |
|        2019 |
|        2017 |
|        2015 |
+-------------+
6 rows in set (0.00 sec)

Following is the query to get current year in MySQL where clause −

mysql> select * from DemoTable1360 where JoiningYear=year(curdate());

This will produce the following output −

+-------------+
| JoiningYear |
+-------------+
|        2019 |
+-------------+
1 row in set (0.00 sec)
raja
Published on 08-Nov-2019 10:35:51
Advertisements