Asheesh Yadav
Asheesh Yadav

Reputation: 41

How can Select date(dd/mm/yyyy) in ascending order in sql

I want To Select stockDate in ASC Order My Table Structure is.:-

enter image description here

Upvotes: 0

Views: 8716

Answers (3)

user6234108
user6234108

Reputation:

SELECT * from inventory_details 
ORDER BY str_to_date(stockDate,'%d/%m/%Y') desc

Upvotes: 2

drunken_monkey
drunken_monkey

Reputation: 1858

Change the date format to YYYY-MM-DD

SELECT * from inventory_details ORDER BY stockDate ASC;

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You have saved the dates as varchar and they are evil, you should use mysql native date data types.

For this you first need to convert the varchar dates to real date using str_to_date function while doing the sorting, here how it works

mysql> select str_to_date('31/07/2015','%d/%m/%Y') as d ;
+------------+
| d          |
+------------+
| 2015-07-31 |
+------------+

So the query becomes

SELECT * from inventory_details 
ORDER BY str_to_date(stockDate,'%d/%m/%Y') desc

Upvotes: 5

Related Questions