Condition to check for due date and current date records in MySQL where clause

To check for such conditions, use IF() in MySQL.

Let us create a table −

Example

mysql> create table demo89
   -> (
   -> duedate date
   -> );
Query OK, 0 rows affected (0.78

Insert some records into the table with the help of insert command −

Example

mysql> insert into demo89 values('2020-01-10');
Query OK, 1 row affected (0.55

mysql> insert into demo89 values(null);
Query OK, 1 row affected (0.13

mysql> insert into demo89 values('2020-11-29');
Query OK, 1 row affected (0.15

mysql> insert into demo89 values('2019-11-29');
Query OK, 1 row affected (0.09

Display records from the table using select statement −

Example

mysql> select *from demo89;

This will produce the following output −

Output

<p>+------------+</p>| duedate    |
<p>+------------+</p>| 2020-01-10 |
<p>| NULL       |</p>| 2020-11-29 |
<p>| 2019-11-29 |</p>+------------+
<p>4 rows in set (0.00 sec)</p>

Following is the query to check for due date and current date records −

Example

mysql> select if (duedate is null
   -> or duedate < curdate(),'Wrong Date',duedate) as Result
   -> from demo89;

This will produce the following output −

Output

<p>+------------+</p>| Result     |
<p>+------------+</p>| Wrong Date |
<p>| Wrong Date |</p>| 2020-11-29 |
<p>| Wrong Date |</p>+------------+
<p>4 rows in set (0.00 sec)</p>
Updated on: 2020-12-11T05:57:50+05:30

744 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements