How to check if a date has passed in MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1340
   -> (
   -> Deadline date
   -> );
Query OK, 0 rows affected (0.43 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1340 values('2019-09-18');
Query OK, 1 row affected (0.52 sec)
mysql> insert into DemoTable1340 values('2019-09-23');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1340 values('2018-12-24');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1340 values('2016-11-01');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1340 values('2019-09-28');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1340;

This will produce the following output −

+------------+
| Deadline   |
+------------+
| 2019-09-18 |
| 2019-09-23 |
| 2018-12-24 |
| 2016-11-01 |
| 2019-09-28 |
+------------+
5 rows in set (0.00 sec)

We should know the current date as well. The current date is as follows −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-09-21 |
+------------+
1 row in set (0.00 sec)

Here is the query to check if a date has passed −

mysql> select * from DemoTable1340 where Deadline >= curdate();

This will produce the following output −

+------------+
| Deadline   |
+------------+
| 2019-09-23 |
| 2019-09-28 |
+------------+
2 rows in set (0.00 sec)
raja
Published on 05-Nov-2019 11:49:46
Advertisements