Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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>
Advertisements
