

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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)
- Related Questions & Answers
- MySQL update column to NULL for blank values
- How to find last date from records with date values in MySQL?
- How to update column values with date records and set 1 for corresponding records before the current date in SQL
- Update all the fields in a table with null or non-null values with MySQL
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Filter dates from a table with DATE and NULL records in MySQL
- Display and concatenate records ignoring NULL values in MySQL
- MySQL Stored Procedure to update records with certain condition?
- How to change the date in a table with date records with MySQL?
- How to update date of datetime field with MySQL?
- MySQL query to select records with a particular date?
- How do I update NULL values in a field in MySQL?
- Display records ignoring NULL in MySQL
- MySQL query to subtract date records with week day and display the weekday with records
- Add 30 days to date in a MySQL table with arrival date records
Advertisements