0

I try to order the results of a query by the date which is in the format yyyy/mm/dd and I am using this query to no avail.

SELECT * FROM table ORDER BY STR_TO_DATE(date, '%y/%m/%d') 

I can't change the type of field that the date is stored in so I am hoping I can order the date post data entry.

Any help and advice appreciated.

3 Answers 3

3

If the date is in that format, you don't need to convert it to a date, surely? That format would sort alphabetically, assuming it is 0 padded... (i.e. July is 3 July 2012 is 2012/07/03...)

So you can just go:

select * from table order by date 

What type of field is your date field: are you sure it is a varchar?

Assuming it is a varchar, you can work out what is going wrong by going:

select str_to_date(date, '%y/%m/%d') from table 

You (should) get all NULL's, because the %y is wrong. Try:

select str_to_date(date, '%Y/%m/%d') from table 

and it should work. But as noted, you don't have to convert to sort.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Because the choice of this date format is usually specifically done because it is naturally sorted (which doesn't mean it's the right choice in a DB which offers date formats).
0

Try with capital Y:

SELECT * FROM table ORDER BY STR_TO_DATE(date, '%Y/%m/%d')

Lowercase Y is for yy, uppercase for yyyy.

Comments

-1

SELECT * FROM table ORDER BY date

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.