STR_TO_DATE
Descriptionβ
The function converts the input datetime string into a DATETIME value based on the specified format.
Syntaxβ
STR_TO_DATE(<datetime_str>, <format>)
Parametersβ
Parameter | Description |
---|---|
<datetime_str> | Required. The input datetime string to be converted. |
<format> | Required. The format string specifying the datetime structure, such as %Y-%m-%d %H:%i:%s or yyyy-MM-dd HH:mm:ss. |
Return Valueβ
- Returns a DATETIME value representing the converted datetime.
- If the input
<datetime_str>
or<format>
is invalid, the function returns NULL.
Exampleβ
Convert common datetime strings to DATETIME
SELECT STR_TO_DATE('2025-01-23 12:34:56', '%Y-%m-%d %H:%i:%s'),STR_TO_DATE('2025-01-23 12:34:56', 'yyyy-MM-dd HH:mm:ss');
+---------------------------------------------------------+-----------------------------------------------------------+
| str_to_date('2025-01-23 12:34:56', '%Y-%m-%d %H:%i:%s') | str_to_date('2025-01-23 12:34:56', 'yyyy-MM-dd HH:mm:ss') |
+---------------------------------------------------------+-----------------------------------------------------------+
| 2025-01-23 12:34:56.000000 | 2025-01-23 12:34:56.000000 |
+---------------------------------------------------------+-----------------------------------------------------------+
Others
select STR_TO_DATE('200442 Monday', '%X%V %W'),STR_TO_DATE('2023','%Y');
+-----------------------------------------+---------------------------+
| str_to_date('200442 Monday', '%X%V %W') | str_to_date('2023', '%Y') |
+-----------------------------------------+---------------------------+
| 2004-10-18 | 2023-01-01 |
+-----------------------------------------+---------------------------+