STR_TO_DATE turns a string into a DATE, DATETIME, or TIME using a supplied format mask.
STR_TO_DATE() parses a character string according to a format mask and returns a DATE, DATETIME, or TIME. Use it when you import CSV files, fix bad data, or compare dates stored as text.
Use STR_TO_DATE(string, format). The format uses the same specifiers as DATE_FORMAT, such as %Y for four-digit year or %d for day of month.See full syntax below.
You often bulk-load Orders where order_date is text like “04/30/2024”. Run an UPDATE with STR_TO_DATE to populate a real DATE column so indexes and date math work.
Yes.In an INSERT … SELECT or LOAD DATA, wrap the text column in STR_TO_DATE so the value lands correctly typed, eliminating a follow-up UPDATE.
Always match the format string to the incoming data exactly; test with SELECT before UPDATE; store as DATE or DATETIME, not VARCHAR; add CHECK constraints to avoid future bad loads.
Mis-matched format masks and implicit timezone assumptions are the top pitfalls. See details below.
.
No. STR_TO_DATE only parses; it does not shift time zones. Store as UTC or use CONVERT_TZ afterward.
MySQL returns NULL and, if sql_mode includes STRICT, raises an error. Use WHERE order_date IS NULL after conversion to catch bad rows.
Yes. Given the same string and format, it always returns the same value. This allows generated columns and functional indexes.