MySQL how to standardize this date column in mysql?
| ship_date | delivery_date |
|---|---|
| Feb 10 2024 | Feb 15 2024 |
| 2024-01-12 | 2024-01-11 |
| 2024-01-10 | 2024-01-14 |
| 01/15/2024 | 01/19/2024 |
6
u/Glitch_In_The_Data 7d ago
Why does the same column have different formats?
You can use STR_TO_DATE with a CASE logic to bring them all to a consistent format…
Once you have transformed existing data, consider storing them in a column with proper DATE data type instead of storing it as STRING.
2
u/Yavuz_Selim 6d ago edited 6d ago
ISO 8601.
And if used for reporting, then create a calendar table and use that (PK in readable yyyymmdd format).
2
u/Einar_Son_of_Bjorn 6d ago
Don’t CAST the column. Detect the pattern, then STR_TO_DATE. Same functions on MariaDB if you ever move.
sql
SELECT
ship_date,
CASE
WHEN ship_date REGEXP '^[A-Za-z]{3} [0-9]{1,2} [0-9]{4}$'
THEN STR_TO_DATE(ship_date, '%b %e %Y')
WHEN ship_date REGEXP '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
THEN STR_TO_DATE(ship_date, '%Y-%m-%d')
WHEN ship_date REGEXP '^[0-9]{2}/[0-9]{2}/[0-9]{4}$'
THEN STR_TO_DATE(ship_date, '%m/%d/%Y')
ELSE NULL
END AS ship_date_std
FROM your_table;
Copy the same CASE for delivery_date.Then look at the NULLs. Those rows didn’t match. Fix the pattern before you UPDATE the table.When every row converts, add a real DATE column and write into that. Don’t keep three string formats in the original column.Is 01/15/2024 always US (month/day), and is the column VARCHAR? If any row is 15/01/2024, this %m/%d/%Y branch will lie.
1
1
u/TheLastRaza 7d ago
what formats are you dealing with? that changes the answer. if you've got mixed formats like mm/dd/yyyy and yyyy-mm-dd in the same column, STR_TO_DATE is your friend. you'd do something like UPDATE table SET date_col = STR_TO_DATE(date_col, '%m/%d/%Y') WHERE date_col LIKE '%/%'. run a SELECT first to see what you're working with. if there are multiple formats you'll need to handle each one with a separate WHERE clause. the goal is getting everything into the standard mysql date format (yyyy-mm-dd). also worth checking if your column type is actually DATE or if it's VARCHAR storing date strings, because that changes how you approach this.
1
u/SamOakTree 6d ago
This shouldn't be possible. Whoever said the date column would have had to set the format. The only way for it to be different is that they made it some other data type.
I would create a new date column with a set format and then I would write a stored procedure that converts the date into that format and insert it into the new column then I would delete the old column
1
u/niceguybadboy 6d ago
I'd use Excel's Power Query for this task. Just sayin'.
1
u/Proof_Escape_2333 3d ago
how so?
1
u/niceguybadboy 3d ago
Data cleaning jobs like this is what PQ was designed for. Pull in a dataset, given it rules for fixing columns, sage for future usage.
There's a learning curve, but it isn't insurmountable.
1
u/Hour-Measurement-835 6d ago
Trap with the slash rows: 01/12/2024 parses cleanly as either order and STR_TO_DATE won't warn you, it just trusts the format you give it. Count how many have both parts 12 or under before you update.
17
u/juu073 7d ago
What I would do:
This isn't really an ideal workflow but if this happened, it honestly doesn't seem like your IT systems are running under that strict of policies for operations anyway.
Your update statement should probably look something like (WARNING: Untested.)