Perform MySQL search between two dates


To perform MySQL search between two dates, use BETWEEN keyword. Let us first create a table −

mysql> create table DemoTable1456
   -> (
   -> CustomerName varchar(30),
   -> StartOfferDate  date,
   -> EndOfferDate date
   -> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1456 values('Chris','2019-09-01','2019-09-30');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1456 values('David','2019-09-01','2019-10-30');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1456 values('Bob','2018-10-01','2018-10-20');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1456;

This will produce the following output −

+--------------+----------------+--------------+
| CustomerName | StartOfferDate | EndOfferDate |
+--------------+----------------+--------------+
| Chris        |    2019-09-01  |   2019-09-30 |
| David        |     2019-09-01 |   2019-10-30 |
| Bob          |    2018-10-01  |   2018-10-20 |
+--------------+----------------+--------------+
3 rows in set (0.00 sec)

Following is the query to perform search between two dates and fetch records −

mysql> select * from DemoTable1456
   -> where date(now())  between StartOfferDate and EndOfferDate;

This will produce the following output −

+--------------+----------------+--------------+
| CustomerName | StartOfferDate | EndOfferDate |
+--------------+----------------+--------------+
| David        | 2019-09-01    |   2019-10-30  |
+--------------+----------------+--------------+
1 row in set (0.04 sec)

Updated on: 10-Dec-2019

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements