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

MySQLMySQLi Database

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

+------------+

| duedate    |

+------------+

| 2020-01-10 |

| NULL       |

| 2020-11-29 |

| 2019-11-29 |

+------------+

4 rows in set (0.00 sec)

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

+------------+

| Result     |

+------------+

| Wrong Date |

| Wrong Date |

| 2020-11-29 |

| Wrong Date |

+------------+

4 rows in set (0.00 sec)

raja
Published on 11-Dec-2020 10:27:30
Advertisements