I’m working on a table with a column, ‘Expiry Date’, as a varchar with all data formatted as DD/MM/YYYY.
The creator of the table has used the wrong type for this expiry date column and now the client needs to filter and show all records before and after the current date as the time. This means the type needs to be changed to date or datetime type to be able to use the CURDATE() function.
However, the current format of the values does not satisfy and wont allow the type to change unless the format is changed to YYYY-MM-DD (or similar).
Is there any way to mass format the values in this column and this column alone as there are thousands of entries and formatting one by one would be extremely time consuming.
2
Answers
What you need is an update on that column, but before doing it I suggest you to check if the result is what you want.
If this returns the results you want you can run the following update:
Of course you will need to replace
expiry_date
andtable_name
with the names of your column and table.Let me assume that you are using MySQL.
Perhaps the simplest method is to add a generated column that is a date:
You can also fix the data:
This will implicitly convert the result of
str_to_date()
to a date, which will be in the YYYY-MM-DD format.More importantly, you can then do:
Here is a db<>fiddle.
You can do similar operations in other databases, but the exact code is a bit different.