
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- How to find last date from records with date values in MySQL?
- MySQL update column to NULL for blank values
- 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
- Filter dates from a table with DATE and NULL records in MySQL
- Display only NOT NULL values from a column with NULL and NOT 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 do I update NULL values in a field in MySQL?
- How to update date of datetime field with MySQL?
- MySQL query to select records with a particular date?
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL query to fetch the latest date from a table with date records
- Add 30 days to date in a MySQL table with arrival date records

Advertisements