Compare date when the AdmissionDate is less than the current date in MySQL


Let us first create a table −

mysql> create table DemoTable
(
   AdmissionDate varchar(50)
);
Query OK, 0 rows affected (0.63 sec)

Note − Let’s say the current date is 14-Sep-2019.

Insert some records in the table using insert command. Following is the query −

mysql> insert into DemoTable values('15-Sep-2019');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('14-Sep-2019');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('13-Sep-2016');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('13-Sep-2019');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('13-Sep-2020');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 15-Sep-2019   |
| 14-Sep-2019   |
| 13-Sep-2016   |
| 13-Sep-2019   |
| 13-Sep-2020   |
+---------------+
5 rows in set (0.00 sec)

Following is the query to compare date when the AdmissionDate is less than the current date −

mysql> select *from DemoTable where str_to_date(AdmissionDate,'%d-%b-%Y') < CURDATE();

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 13-Sep-2016   |
| 13-Sep-2019   |
+---------------+
2 rows in set (0.00 sec)
raja
Published on 10-Oct-2019 15:10:25
Advertisements