Check if the current date falls in a given date range using MySQL query


Let us first create a table −

mysql> create table DemoTable1448
   -> (
   -> StartDate date,
   -> EndDate date
   -> );
Query OK, 0 rows affected (0.46 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1448 values('2019-01-21','2019-03-22');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1448 values('2019-04-05','2019-10-10');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1448 values('2019-10-01','2019-10-29');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1448 values('2018-12-31','2019-12-31');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1448;

This will produce the following output −

+------------+------------+
| StartDate  | EndDate    |
+------------+------------+
| 2019-01-21 | 2019-03-22 |
| 2019-04-05 | 2019-10-10 |
| 2019-10-01 | 2019-10-29 |
| 2018-12-31 | 2019-12-31 |
+------------+------------+
4 rows in set (0.00 sec)

Let’s say the current date is −

2019-10-05

Following is the query to check if the current date falls in a given date range −

mysql> select (curdate() >=StartDate and curdate() <=EndDate) as DateInRange from DemoTable1448;

This will produce the following output −

+-------------+
| DateInRange |
+-------------+
|           0 |
|           1 |
|           1 |
|           1 |
+-------------+
4 rows in set (0.04 sec)

Updated on: 10-Dec-2019

669 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements