How to use CONTAINS() with CURDATE in MySQL?


For this, you can use CONCAT() with CURDATE().There is no function with the name CONTAINS() in MySQL.

Let us first get the current date. The current date is as follows −

mysql> select curdate();

This will produce the following output −

+------------+
| curdate()  |
+------------+
| 2019-11-28 |
+------------+
1 row in set (0.00 sec)

We will now create a table −

mysql> create table DemoTable1803
     (
     Name varchar(20),
     JoiningYear varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1803 values('Chris','2020/2017');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1803 values('David','2017/2018');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1803 values('Adam','2018/2019');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1803;

This will produce the following output −

+-------+-------------+
| Name  | JoiningYear |
+-------+-------------+
| Chris | 2020/2017   |
| David | 2017/2018   |
| Adam  | 2018/2019   |
+-------+-------------+
3 rows in set (0.00 sec)

Here is the query

mysql> select * from DemoTable1803 where JoiningYear like concat('%', YEAR(CURDATE()), '%');

This will produce the following output −

+------+-------------+
| Name | JoiningYear |
+------+-------------+
| Adam | 2018/2019   |
+------+-------------+
1 row in set (0.00 sec)

Updated on: 24-Dec-2019

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements