MySQL query to select all data between range of two dates?

MySQLMySQLi Database

To select all data between range of two dates, use MySQL BETWEEN −

select * from yourTableName where yourColumnName between yourDateValue1 and yourDateValue2;

Let us first create a −

mysql> create table DemoTable1422
   -> (
   -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> EmployeeName varchar(20),
   -> EmployeeJoiningDate date
   -> );
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('John','2017-09-28');
Query OK, 1 row affected (0.39 sec)
mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Chris','2019-09-29');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Bob','2018-01-31');
Query OK, 1 row affected (0.55 sec)
mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Sam','2018-12-21');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1422(EmployeeName,EmployeeJoiningDate) values('Mike','2019-11-10');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select −

mysql> select * from DemoTable1422;

This will produce the following output −

+------------+--------------+---------------------+
| EmployeeId | EmployeeName | EmployeeJoiningDate |
+------------+--------------+---------------------+
|          1 | John         |       2017-09-28    |
|          2 | Chris        |       2019-09-29    |
|          3 | Bob          |       2018-01-31    |
|          4 | Sam          |       2018-12-21    |
|          5 | Mike         |       2019-11-10    |
+------------+--------------+---------------------+
5 rows in set (0.00 sec)

Following is the query to select all data between range of two dates −

mysql> select * from DemoTable1422 where EmployeeJoiningDate between '2018-09-29' and '2019-09-29';

This will produce the following output −

+------------+--------------+---------------------+
| EmployeeId | EmployeeName | EmployeeJoiningDate |
+------------+--------------+---------------------+
|          2 | Chris        |      2019-09-29     |
|          4 | Sam          |       2018-12-21    |
+------------+--------------+---------------------+
2 rows in set (0.00 sec)
raja
Published on 12-Nov-2019 05:38:41
Advertisements