I found a surprisingly easy way to deal with this problem.
I began with ugly things having various date styles in an excel spreadsheet, so I corrected some the dates manually into another copied field (date_ok).
Here's the table, at this stage, with some data:
CREATE TABLE a_few_dattes ( -- double t intended: I'm currently eating dattes
date_original text,
date_ok text
);
INSERT INTO a_few_dattes VALUES ('08/08/2024', '2024-08-08');
INSERT INTO a_few_dattes VALUES ('08/13/2024', '2024-08-13');
INSERT INTO a_few_dattes VALUES ('45565', '2024-09-30');
INSERT INTO a_few_dattes VALUES ('09/03/2024', '2024-09-03');
INSERT INTO a_few_dattes VALUES ('08/29/2024', '2024-08-29');
INSERT INTO a_few_dattes VALUES ('07/09/2024', '07/09/2024');
INSERT INTO a_few_dattes VALUES ('09/14/2024', '2024-09-14');
INSERT INTO a_few_dattes VALUES ('10/05/2024', '2024-10-05');
INSERT INTO a_few_dattes VALUES ('10/08/2024', '2024-10-08');
INSERT INTO a_few_dattes VALUES ('10/12/2024', '2024-10-12');
INSERT INTO a_few_dattes VALUES ('07/11/2024', '07/11/2024');
INSERT INTO a_few_dattes VALUES ('10/19/2024', '2024-10-19');
Here's the table:
SELECT * FROM a_few_dattes;
┌───────────────┬────────────┐
│ date_original │ date_ok │
├───────────────┼────────────┤
│ 08/08/2024 │ 2024-08-08 │
│ 08/13/2024 │ 2024-08-13 │
│ 45565 │ 2024-09-30 │
│ 09/03/2024 │ 2024-09-03 │
│ 08/29/2024 │ 2024-08-29 │
│ 07/09/2024 │ 07/09/2024 │
│ 09/14/2024 │ 2024-09-14 │
│ 10/05/2024 │ 2024-10-05 │
│ 10/08/2024 │ 2024-10-08 │
│ 10/12/2024 │ 2024-10-12 │
│ 07/11/2024 │ 07/11/2024 │
│ 10/19/2024 │ 2024-10-19 │
└───────────────┴────────────┘
At this point, I ended up with various date formats. I'd just like to have some uniform formats.
Then I simply made another field with a date type, and updated it from the date_ok field with just a simple type cast:
ALTER TABLE a_few_dattes ADD COLUMN date_iso date;
UPDATE a_few_dattes SET date_iso = date_ok::date;
And here's the result:
SELECT * FROM a_few_dattes;
┌───────────────┬────────────┬────────────┐
│ date_original │ date_ok │ date_iso │
├───────────────┼────────────┼────────────┤
│ 08/08/2024 │ 2024-08-08 │ 2024-08-08 │
│ 08/13/2024 │ 2024-08-13 │ 2024-08-13 │
│ 45565 │ 2024-09-30 │ 2024-09-30 │
│ 09/03/2024 │ 2024-09-03 │ 2024-09-03 │
│ 08/29/2024 │ 2024-08-29 │ 2024-08-29 │
│ 07/09/2024 │ 07/09/2024 │ 2024-09-07 │
│ 09/14/2024 │ 2024-09-14 │ 2024-09-14 │
│ 10/05/2024 │ 2024-10-05 │ 2024-10-05 │
│ 10/08/2024 │ 2024-10-08 │ 2024-10-08 │
│ 10/12/2024 │ 2024-10-12 │ 2024-10-12 │
│ 07/11/2024 │ 07/11/2024 │ 2024-11-07 │
│ 10/19/2024 │ 2024-10-19 │ 2024-10-19 │
└───────────────┴────────────┴────────────┘
Time to get rid of the ugly stuff:
ALTER TABLE a_few_dattes DROP COLUMN date_original;
ALTER TABLE a_few_dattes DROP COLUMN date_ok;
And here's the final, clean, result:
SELECT * FROM a_few_dattes;
┌────────────┐
│ date_iso │
├────────────┤
│ 2024-08-08 │
│ 2024-08-13 │
│ 2024-09-30 │
│ 2024-09-03 │
│ 2024-08-29 │
│ 2024-09-07 │
│ 2024-09-14 │
│ 2024-10-05 │
│ 2024-10-08 │
│ 2024-10-12 │
│ 2024-11-07 │
│ 2024-10-19 │
└────────────┘
I think that what happened behind the scene was that PostgreSQL interpreted the dates as valid dates, even though they had different formats, and then converted the seamlessly into ISO-8601.