

- 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 query to order timestamp in descending order but place the timestamp 0000-00-00 00:00:00 first?
Let us first create a table &mnus;
mysql> create table DemoTable ( `timestamp` timestamp ); Query OK, 0 rows affected (1.12 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(now()); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('00:00:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-01-10 12:34:45'); Query OK, 1 row affected (0.80 sec) mysql> insert into DemoTable values('2019-12-31 10:50:45'); Query OK, 1 row affected (0.84 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | timestamp | +---------------------+ | 2019-08-17 06:17:12 | | 0000-00-00 00:00:00 | | 2018-01-10 12:34:45 | | 2019-12-31 10:50:45 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to order the timestamp in descending order by first displaying the 0 timestamp −
mysql> select *from DemoTable order by (`timestamp`=0) DESC,`timestamp` DESC;
This will produce the following output −
+---------------------+ | timestamp | +---------------------+ | 0000-00-00 00:00:00 | | 2019-12-31 10:50:45 | | 2019-08-17 06:17:12 | | 2018-01-10 12:34:45 | +---------------------+ 4 rows in set (0.00 sec)
- Related Questions & Answers
- Create MySQL datetime column with default 0000-00-00?
- How can I store ‘0000-00-00’ as a date in MySQL?
- MySQL query to select date from 00:00 to today’s date
- In which format Year(2) or Year(4) MySQL will return the value of year from date ‘0000-00-00’?
- Display hour in KK (00-11) format in Java
- Format hour in HH (00-23) format in Java
- Convert PHP variable “11:00 AM” to MySQL time format?
- Construct ∈-NFA of Regular Language L = (00)*1(11)*
- 8085 Program to alternately display 00 and FF in the data field
- Construct ∈-NFA of Regular Language L = (0+1)*(00+ 11)
- Program to alternately display 00 and FF in the data field in 8085 Microprocessor
- How to order by timestamp (descending order) in MongoDB
- MySQL command to order timestamp values in ascending order without using TIMESTAMP()?
- MySQL command to order timestamp values in ascending order?
- How to order by timestamp in MySQL?
Advertisements