- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL queries to update date records with NULL values
You can use IFNULL() for this. Let us first create a table −
mysql> create table DemoTable -> ( -> added_date date, -> updated_date date -> ); Query OK, 0 rows affected (0.95 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-10','2019-06-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-05-19',NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL,'2019-09-05'); 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 −
+------------+--------------+ | added_date | updated_date | +------------+--------------+ | 2019-01-10 | 2019-06-01 | | 2019-05-19 | NULL | | NULL | 2019-09-05 | +------------+--------------+ 3 rows in set (0.00 sec)
Case 1 − If you want the result in descending order.
Here is the query to select row with added_date and updated_date.
mysql> select *from DemoTable order by ifnull(updated_date,added_date) desc;
This will produce the following output −
+------------+--------------+ | added_date | updated_date | +------------+--------------+ | NULL | 2019-09-05 | | 2019-01-10 | 2019-06-01 | | 2019-05-19 | NULL | +------------+--------------+ 3 rows in set (0.00 sec)
Case 2 − If you want the result in ascending order.
Here is the query to select row with added_date and updated_date −
mysql> select *from DemoTable order by ifnull(updated_date,added_date);
This will produce the following output −
+------------+--------------+ | added_date | updated_date | +------------+--------------+ | 2019-05-19 | NULL | | 2019-01-10 | 2019-06-01 | | NULL | 2019-09-05 | +------------+--------------+ 3 rows in set (0.00 sec)
Advertisements