

- 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
How to Order by date in MySQL but place empty dates in the end?
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)
- Related Questions & Answers
- How to order records by a column in MySQL and place empty records at the end?
- Order By date ASC in MySQL?
- How to order by date and time in MySQL?
- MySQL query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
- MySQL ORDER BY Date field not in date format?
- How to place number 0 from a column at the end maintaining the ascending search order in MySQL?
- MySQL query to order and display difference between dates from the current date
- Order by date set with varchar type in MySQL
- Sort by date & time in descending order in MySQL?
- Update table and order dates in MySQL
- Select the date records between two dates in MySQL
- How to ORDER BY RELEVANCE in MySQL?
- How to ORDER BY LIKE in MySQL?
- How to order by timestamp in MySQL?
- How to order by auto_increment in MySQL?
Advertisements