
- 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
Order by date set with varchar type in MySQL
For this, use ORDER BY STR_TO_DATE in MySQL as in the below syntax −
select *from yourTableName ORDER BY STR_TO_DATE(yourColumnName, '%M %Y') DESC;
Let us first create a table −
mysql> create table DemoTable678(DueDate varchar(200)); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command. We have inserted dates here −
mysql> insert into DemoTable678 values('March 2019'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable678 values('November 2018'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable678 values('January 2019'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable678;
This will produce the following output −
+---------------+ | DueDate | +---------------+ | March 2019 | | November 2018 | | January 2019 | +---------------+ 3 rows in set (0.00 sec)
Following is the query to order by date set with varchar type −
mysql> select *from DemoTable678 ORDER BY STR_TO_DATE(DueDate, '%M %Y') DESC;
This will produce the following output −
+---------------+ | DueDate | +---------------+ | March 2019 | | January 2019 | | November 2018 | +---------------+ 3 rows in set (0.00 sec)
- Related Articles
- Find date record after a particular date from a column with VARCHAR type in MySQL
- Order By date ASC in MySQL?
- Order VARCHAR records with string and numbers in MySQL
- MySQL ORDER BY Date field not in date format?
- Convert varchar to date in MySQL?
- Set a certain value first with MySQL ORDER BY?
- Select with set order in MySQL
- Sort by date & time in descending order in MySQL?
- How to order by date and time in MySQL?
- Is it okay to store double and date in VARCHAR with MySQL?
- Comparison of varchar date records from the current date in MySQL
- Convert VARCHAR data to MySQL date format?
- Convert VARCHAR Date to a different format in MySQL?
- How to set a string with hyphen and numbers in MySQL varchar?
- MySQL Order by with case?

Advertisements