How to validate expiry date on a MySQL query?


You can use NOW() for this. Following is the syntax −

select * from yourTableName
where yourColumnName> now();

Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   expiryDateOfMedicine datetime
   );
Query OK, 0 rows affected (0.55 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-27 11:29:00');
Query OK, 1 row affected (0.36 sec)
mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-26 10:39:21');
Query OK, 1 row affected (0.41 sec)
mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-28 11:30:10');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(expiryDateOfMedicine) values('2019-04-29 12:44:11');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+----+----------------------+
| Id | expiryDateOfMedicine |
+----+----------------------+
| 1  | 2019-04-27 11:29:00  |
| 2  | 2019-04-26 10:39:21  |
| 3  | 2019-04-28 11:30:10  |
| 4  | 2019-04-29 12:44:11  |
+----+----------------------+
4 rows in set (0.00 sec)

Following is the query to validate expiry date on a MySQL query −

mysql> select * from DemoTable
where expiryDateOfMedicine > now();

This will produce the following output −

+----+----------------------+
| Id | expiryDateOfMedicine |
+----+----------------------+
| 3  | 2019-04-28 11:30:10  |
| 4  | 2019-04-29 12:44:11  |
+----+----------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements