Tutorialspoint

How to check whether now() falls between two specific dates in MySQL?


1 Answer
Ankith Reddy

Here, now() represents the current date. To check whether it falls between two specific dates, you need to use the BETWEEN. Let us first create a table −

mysql> create table DemoTable
   (
   FirstDate datetime,
   SecondDate datetime
   );
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-04-01','2019-05-02');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values('2019-05-28','2019-06-04');
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable values('2016-01-31','2019-03-01');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-----------------------+-----------------------+
| FirstDate             | SecondDate            |
+-----------------------+-----------------------+
| 2019-04-01 00 :00 :00 | 2019-05-02 00 :00 :00 |
| 2019-05-28 00 :00 :00 | 2019-06-04 00 :00 :00 |
| 2016-01-31 00 :00 :00 | 2019-03-01 00 :00 :00 |
+-----------------------+-----------------------+
3 rows in set (0.00 sec)

Following is the query to check now() falls between two specific dates −

mysql> select *from DemoTable where now() BETWEEN FirstDate AND SecondDate;

Output

+-----------------------+-----------------------+
| FirstDate             | SecondDate            |
+-----------------------+-----------------------+
| 2019-05-28 00 :00 :00 | 2019-06-04 00 :00 :00 |
+-----------------------+-----------------------+
1 row in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.