When I run DATE_FORMAT('test', '%W %M %Y')
I get null
returned.
I’m running an update to my table extras
where the column is a nullable varchar, but when I run eg.
update extras
set extras.`value` = DATE_FORMAT('test', '%W %M %Y');
I get the following error:
[22001][1292] Data truncation: Incorrect datetime value: 'test'
extras.value
is a varchar column with datetime values some of which are not valid dates. I want to update the column to null when the datetime is invalid ie. just a string as in this case ‘test’.
2
Answers
When the STRICT_TRANS_TABLES sql_mode is enabled, any date/time parsing error becomes a fatal error. You have to disable it or use a regex to validate the date string (which is very messy) before using it as a date.
fiddle
Check does the value is valid date with regular expression.
Example – the most simple pattern which does not check for value validity (and allows, for example, ‘2022-25-78’, in this case the whole UPDATE will fail):
fiddle