SQL STR_TO_DATE function
Returns a date or datetime parsed from a string according to the mask you supply, MySQL's counterpart to Oracle's TO_DATE.
- Returns
date / datetime- In this category
- 31 of 121
語法
STR_TO_DATE('15/01/2024', '%d/%m/%Y') -- MySQL
TO_DATE('15/01/2024', 'DD/MM/YYYY') -- Oracle
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | PARSE or CONVERT with a style number |
| PostgreSQL | Different name or syntax | to_date(s, mask) with DD/MM/YYYY-style masks |
| MySQL | 是 | Supported under this name. |
| SQLite | Not built in | no mask parsing; only ISO-8601 text works implicitly |
| Oracle | Different name or syntax | TO_DATE |
範例
MySQL
SELECT STR_TO_DATE('15/01/2024', '%d/%m/%Y') AS parsed;
Oracle
SELECT TO_DATE('15/01/2024', 'DD/MM/YYYY') AS parsed FROM dual;
SQL Server
SELECT PARSE('15/01/2024' AS date USING 'en-GB') AS parsed;
小知識
- In a SELECT, bad input returns NULL with a warning: a %m/%d/%Y mask against 15/01/2024 yields NULL. Inside an INSERT under strict mode the same call fails with error 1411.
- MySQL's masks use %Y-%m-%d; PostgreSQL and Oracle use YYYY-MM-DD-style words. Copying masks between engines parses nothing.
- Two-digit years follow MySQL's sliding rule: 00-69 becomes 2000-2069, 70-99 becomes 1970-1999.
Questions about STR_TO_DATE
Why does STR_TO_DATE return NULL?
The mask did not match the string (separators or field order differ), or the result is not a real date, such as 2024-02-30. SHOW WARNINGS names the value it rejected.
Is there a SQLite STR_TO_DATE?
No. SQLite only parses ISO-8601 text; anything else must be reshaped in SQL or the application first.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Parse text into a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Safely convert strings to dates in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.