Converting "string" dates with errors to normal dates or NULL I have an old table containing dates stored as strings like '20191001'.
I have created a new field of type DATE and I try to recover the old dates in the new field :

UPDATE extcg SET newdate=to_date(olddate,'yyyymmdd');


The problem is that some of these dates are invalid (20190231). I would like to skip the conversion for these dates and leave the new field NULL.

Is there a way to exclude these in a WHERE statement ?
For those interested, here is a function that does the job :

CREATE FUNCTION to_date_or_null(ADate TEXT, AFormat TEXT) RETURNS DATE AS
$BODY$
BEGIN
    RETURN to_date(ADate,AFormat);
EXCEPTION
    WHEN others THEN RETURN NULL;
END;
$BODY$ LANGUAGE plpgsql IMMUTABLE STRICT;


I used it this way :

UPDATE extcg SET newdate=to_date_or_null(olddate,'yyyymmdd');


And thanks to @Luuk for mastering Google searches ;)