SQL ORDER BY date problem

Viewed 413965

Can you please help me in solving this problem. I am trying to order the results of an SQL query by date, but I'm not getting the results I need.

The query I'm using is:

SELECT date FROM tbemp ORDER BY date ASC

Results are:

01/02/2009
03/01/2009
04/06/2009
05/03/2009
06/12/2008
07/02/2009

Results should be:

06/12/2008
03/01/2009
01/02/2009
07/02/2009

I need to select the date in the format above.

Your help is much appreciated.

12 Answers

this should work for your date format

order by convert(date, your_column, 104) desc

Casting/Converting can result in out of range exceptions that unfortunately are not always as simple as excluding nulls.

A simple alternative method, which avoids the cast, is:

SELECT date
FROM table
ORDER BY YEAR(date), MONTH(date), DAY(date) ASC;
Related