MySQL date comparison to fetch dates between a given range?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   AdmissionDate date
);
Query OK, 0 rows affected (0.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-08-31');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('2019-09-01');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2019-05-10');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('2019-06-12');
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-08-31    |
| 2019-09-01    |
| 2019-05-10    |
| 2019-06-12    |
+---------------+
4 rows in set (0.00 sec)

Following is the query to perform date comparison to fetch dates between a given range −

mysql> select *from DemoTable where AdmissionDate >='2019-06-01' and AdmissionDate <='2019-08-31';

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-08-31    |
| 2019-06-12    |
+---------------+
2 rows in set (0.00 sec)
raja
Published on 03-Oct-2019 09:49:25
Advertisements