Divern
Divern

Reputation: 363

How to display an sql date in order when using ORDER BY?

I am trying to display the following dates in order from smallest to biggest.

They are stored this way.

02-12-15
02-11-13
12-20-12
12-12-12
05-12-17

This is the following code I use

"SELECT ledate FROM `points_bar` WHERE student_id = '$studentID' ORDER BY ledate ASC"; 

And this is what I want to transform it to.

02-11-13
12-20-12
12-12-12
02-12-15
05-12-17

Can someone please point out to me a possible solution or give me a little push?

Upvotes: 0

Views: 44

Answers (2)

Ravi
Ravi

Reputation: 1172

just cast ledate as date......

 SELECT ledate FROM `points_bar` 
 WHERE student_id = '$studentID' 
 ORDER BY cast(ledate as date) ASC

Upvotes: 0

Al Amin Chayan
Al Amin Chayan

Reputation: 2500

Your dates are not in mysql supported date format. It treat as varchar. Change the SQL as follows should work.

"SELECT ledate FROM `points_bar` WHERE student_id = '$studentID' ORDER BY STR_TO_DATE(`ledate`, '%m-%d-%y') ASC";

See STR_TO_DATE

Upvotes: 2

Related Questions