Skip to content
Advertisement

MySQL ORDER BY Date field which is not in date format

I have a field containing dates in this format DD/MM/YYYY and I need to order the results DESC by this field, but it is saved as a VARCHAR and I cannot change this. Is there a workaround?

There really is no way for me to change the field type so please don’t say this is a bad way to do this as I already know. I just need to know if it is possible.

Thanks for any help and advice in advance.

Advertisement

Answer

You can do it by the following way,

SELECT ...
FROM ...
ORDER BY STR_TO_DATE(yourDate,'%d-%m-%Y') DESC
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement