
- 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 ORDER BY Date field not in date format?
The following is the syntax to order by date field which is not in date format
select *from yourTableName order by STR_TO_DATE(yourColumnName,'%d/%m/%Y') DESC;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table orderByDateFormatDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ArrivalDatetime varchar(100) -> ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('01/10/2012'); Query OK, 1 row affected (0.20 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('03/11/2010'); Query OK, 1 row affected (0.15 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('04/09/2018'); Query OK, 1 row affected (0.14 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('31/01/2019'); Query OK, 1 row affected (0.17 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('21/04/2017'); Query OK, 1 row affected (0.21 sec) mysql> insert into orderByDateFormatDemo(ArrivalDatetime) values('23/05/2016'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from orderByDateFormatDemo;
The following is the output
+----+-----------------+ | Id | ArrivalDatetime | +----+-----------------+ | 1 | 01/10/2012 | | 2 | 03/11/2010 | | 3 | 04/09/2018 | | 4 | 31/01/2019 | | 5 | 21/04/2017 | | 6 | 23/05/2016 | +----+-----------------+ 6 rows in set (0.00 sec)
Here is the query to ORDER BY date field which is not in date format
mysql> select *from orderByDateFormatDemo -> order by -> STR_TO_DATE(ArrivalDatetime,'%d/%m/%Y') DESC;
The following is the output
+----+-----------------+ | Id | ArrivalDatetime | +----+-----------------+ | 4 | 31/01/2019 | | 3 | 04/09/2018 | | 5 | 21/04/2017 | | 6 | 23/05/2016 | | 1 | 01/10/2012 | | 2 | 03/11/2010 | +----+-----------------+ 6 rows in set (0.04 sec)
- Related Articles
- Order By date ASC in MySQL?
- Convert Date not in a proper format with MySQL?
- Extract Numeric Date Value from Date Format in MySQL?
- Sort by date & time in descending order in MySQL?
- Order by date set with varchar type in MySQL
- How to order by date and time in MySQL?
- How to convert Python date format to 10-digit date format for mysql?
- Convert MySQL Unix-Timestamp format to date format?
- Insert current date in datetime format MySQL?
- Set a specific Date Format in MySQL?
- Format date while inserting records in MySQL
- MySQL extract year from date format?
- Replace date format with MySQL STR_TO_DATE
- Format date to display month names with the entire date in MySQL?
- How to correctly convert a date format into a MySQL date?

Advertisements