Using MAX and DATE_FORMAT to get the latest date from a MySQL table

Don’t use DATE_FORMAT when trying to assertain the latest date in a table. It won’t work. DATE_FORMAT will convert the dates to a string, and then the MAX will compare the strings, not the dates.

SELECT MAX(DATE_FORMAT(date_field, '%d-%m-%Y')) AS latest_dateĀ 
FROM table;

Is NOT the same as;

SELECT MAX(date_field) AS latest_dateĀ 
FROM table;

The latter is the correct way of doing it.

 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>