How to Order by date in MySQL but place empty dates in the end?

MySQLMySQLi Database

Order by date and set the empty dates in the last with the help of ORDER BY clause and IS NULL property. The syntax is as follows:

SELECT *FROM yourTableName
ORDER BY (yourDateColumnName IS NULL), yourDateColumnName DESC;

In the above syntax, we will sort the NULL first after that date. To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table DateColumnWithNullDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> LoginDateTime datetime,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(date_add(now(),interval -1 year));
Query OK, 1 row affected (0.15 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(NULL);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(NULL);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(now());
Query OK, 1 row affected (0.18 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(curdate());
Query OK, 1 row affected (0.23 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values('2017-08-25 15:30:35');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(NULL);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values('2016-12-25 16:55:55');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values(NULL);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values('2014-11-12 10:20:23');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DateColumnWithNullDemo(LoginDateTime) values('2020-01-01 06:45:23');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from DateColumnWithNullDemo;

The following is the output:

+----+---------------------+
| Id | LoginDateTime       |
+----+---------------------+
|  1 | 2018-01-29 17:07:20 |
|  2 | NULL                |
|  3 | NULL                |
|  4 | 2019-01-29 17:07:54 |
|  5 | 2019-01-29 00:00:00 |
|  6 | 2017-08-25 15:30:35 |
|  7 | NULL                |
|  8 | 2016-12-25 16:55:55 |
|  9 | NULL                |
| 10 | 2014-11-12 10:20:23 |
| 11 | 2020-01-01 06:45:23 |
+----+---------------------+
11 rows in set (0.00 sec)

Here is the query to set the NULL value at last and sort the date in descending order:

mysql> select *from DateColumnWithNullDemo
   -> order by (LoginDateTime IS NULL), LoginDateTime DESC;

The following is the output:

+----+---------------------+
| Id | LoginDateTime       |
+----+---------------------+
| 11 | 2020-01-01 06:45:23 |
|  4 | 2019-01-29 17:07:54 |
|  5 | 2019-01-29 00:00:00 |
|  1 | 2018-01-29 17:07:20 |
|  6 | 2017-08-25 15:30:35 |
|  8 | 2016-12-25 16:55:55 |
| 10 | 2014-11-12 10:20:23 |
|  2 | NULL                |
|  3 | NULL                |
|  7 | NULL                |
|  9 | NULL                |
+----+---------------------+
11 rows in set (0.00 sec)
raja
Published on 26-Feb-2019 10:05:38
Advertisements