I have a mysql table with a VARCHAR field that stores the dates in this format dd/mm/yyyy. This table already have data stored in it, and I am required to sort this data in a descending order. Is this possible?
I have a mysql table with a VARCHAR field that stores the dates in
Share
The best way to deal with dates is to store then as a date data type.
You can easily convert the data that is in them now into a date column by adding a column of date datatype, merging the data into it and then re-naming it.
If you do this, you will be able to do all sorts of complex date stuff (not limited to just sorting a date nicely, or getting the data out in different formats – but calculations, comparisons and much more). You will need to be aware that you will also have to probably change how you insert data into the column – though there are many easy ways to convert a string-date from a CSV or form into a proper date column.
Edit: As Martin points out, if you have some bum data stored in there now, it might cause problems when converting to a date, but you can find these reasonably easily when doing the update. After the update… query is run, simply run the following:
This will show you the rows that didn’t update