Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MySQL Date Comparison to Fetch Dates Between a Given Range



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)
Updated on: 2019-10-03T06:20:15+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements