Filter query by current date in MySQL


Let us first create a table −

mysql> create table DemoTable(DueDate datetime);
Query OK, 0 rows affected (0.94 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-07-10 04:20:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-07-10 05:10:40');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('2019-07-10 09:00:20');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2019-07-10 10:01:04');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('2019-07-10 12:11:10');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-07-10 04:20:00 |
| 2019-07-10 05:10:40 |
| 2019-07-10 09:00:20 |
| 2019-07-10 10:01:04 |
| 2019-07-10 12:11:10 |
+---------------------+
5 rows in set (0.00 sec)

Following is the query to filter by current date −

mysql> select *from DemoTable where date(DueDate)=curdate()
   and time(DueDate) > '04:00:00'
   and time(DueDate) < '10:00:00';

This will produce the following output −

+---------------------+
| DueDate             |
+---------------------+
| 2019-07-10 04:20:00 |
| 2019-07-10 05:10:40 |
| 2019-07-10 09:00:20 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 22-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements