STR_TO_DATE() in MySQL — How It Works

What the MySQL STR_TO_DATE() function does, with an example.

Published October 19, 2026

STR_TO_DATE(str, format) converts a string into a date or datetime using the given format specifiers. It returns NULL if the string does not match.

How it works

It is the inverse of DATE_FORMAT and useful when importing data.

Try it in MySQL

SELECT STR_TO_DATE('12/11/2026', '%d/%m/%Y')
2026-11-12
SELECT STR_TO_DATE('Nov 12 2026', '%b %d %Y')
2026-11-12
SELECT STR_TO_DATE('bad', '%Y')
NULL

Practical examples

Example 1

SELECT STR_TO_DATE('12/11/2026', '%d/%m/%Y');
-- 2026-11-12

Example 2

SELECT STR_TO_DATE('Nov 12 2026', '%b %d %Y');
-- 2026-11-12

Example 3

SELECT STR_TO_DATE('bad', '%Y');
-- NULL

Use it in a real query

SELECT id, STR_TO_DATE('12/11/2026', '%d/%m/%Y') AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Wrong format specifiers returning NULL.
  • Strict SQL mode errors on invalid dates.
  • Ambiguous day/month order.

Key takeaways

  • STR_TO_DATE() in one line: STR_TO_DATE(str, format) converts a string into a date or datetime using the given format specifiers.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

Why NULL?

The string doesn't match the format.

Does STR_TO_DATE() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles