I have a table which is populated with data from an external source. The problem is I am getting two different format of dates for the same column, there are some records in '%Y-%m-%d %H:%i' format and other in '%Y-%d-%m %H:%i%s' format and they are all VARCHAR type.
I know how to convert a string type date field to a date/datetime field but how do I handle discrepancies in the way the dates are coming? Is it possible to Update dates on the basis of their individual format, so that I can apply a WHERE condition for updates to take place ONLY WHERE date_field is of '%Y-%m-%d %H:%i' format, and then another query to make updates for date field in '%Y-%d-%m %H:%i%s' format?
Right now when I try updating the tables with a common query I get error for the fields which donot match the format:
UPDATE my_table
SET my_date_field = STR_TO_DATE(my_date_field,'%Y-%m-%d %H:%i:%s');
RESPONSE: Error Code: 1411. Incorrect datetime value: ’10-22-12 15:00′ for function str_to_date
UPDATE my_table
SET my_date_field = STR_TO_DATE(my_date_field,'%Y-%d-%m %H:%i');
RESPONSE: Error Code: 1292. Truncated incorrect datetime value: ‘2010-01-01 00:00:00’
for this value
10-22-12 15:00you should use,%y-%d-%m %H:%iwhile for
2010-01-01 00:00:00, it should be%Y-%m-%d %H:%i:%sso your query will use
CASEOther Source