Skip to content
Advertisement

SQL Update record when field is in the past

how can I set all records in the field “published”=0 whose date in field F12 is older than today?

Unfortunately, the field F12 is not a DateTime field, but a string. Unfortunately, I can’t change that. I have to convert the string into a date first.

Unfortunately, this code does not work

UPDATE 'mytable' 
SET 'published' = '0' 
WHERE STR_TO_DATE('F12', '%d.%m.%Y') DATEDIFF(day, 'F12', NOW()) >= 1

A screenshot of phpMyAdmin is here:

enter image description here

Thank you very much for your support and best regards

Advertisement

Answer

I would expect logic like this:

update mytable
    set published = 0
    where STR_TO_DATE(F12, '%d.%m.%Y') < curdate();

Note that single quotes are being used only for the string constant. That could be the issue with your query. Or it could be that you are using now() which has a time component so it also updates values whose date is today.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement