MySQL select * and find record with current date

MySQLMySQLi Database

For the current date, use CURDATE(). Also, use STR_TO_DATE() to format date and compare it with the current date as in the below syntax −

Syntax

select *from yourTableName where str_to_date(yourColumnName,'yourFormatSpecifier')=curdate();

Let’s say the current date is 27/10/2019.

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> JoiningDate varchar(40)
   -> );
Query OK, 0 rows affected (0.79 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('27/10/2017');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('27/10/2018');
Query OK, 1 row affected (0.78 sec)
mysql> insert into DemoTable values('27/10/2019');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('27/10/2020');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement−

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| JoiningDate |
+-------------+
| 27/10/2017  |
| 27/10/2018  |
| 27/10/2019  |
| 27/10/2020  |
+-------------+
4 rows in set (0.00 sec)

Following is the query to compare and find a record with the current date −

mysql> select *from DemoTable where str_to_date(JoiningDate,'%d/%m/%Y')=curdate();

This will produce the following output −

+-------------+
| JoiningDate |
+-------------+
| 27/10/2019  |
+-------------+
1 row in set (0.00 sec)
raja
Published on 13-Dec-2019 10:29:12
Advertisements