Order by date in MySQL

Suppose you write a query to sort a table by date field.

SELECT aDate FROM aTable ORDER BY aDate

The above query should sort aDate column in ascending order. But it doesnt work the way it should.
To sort date field in MySQL use following query:

SELECT DATE_FORMAT(aDate, '%d-%M-%Y') AS aDate, aDate AS sDate FROM aTable ORDER BY sDate
Advertisements

8 thoughts on “Order by date in MySQL

  1. But if the date filed was of the type ‘datetime’, the first query works as expected.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s