Skip to content

TO_DATE()

Function Description

TO_DATE() function converts a string to a date or date-time type according to the specified date or time display format.

A format string can contain literal characters and format specifiers starting with %. The literal characters in format must match the literal characters in str. The format specifier in format must match the date or time part in str.

Function Syntax

> TO_DATE(str,format)

Parameter definition

Arguments Description
str Required.
If str is NULL, the function returns NULL.
If the date or datetime value from str is invalid, TO_DATE() will return NULL with a warning.
format Optional parameters. A format string representing the format of the return value.
If format is omitted, a DATETIME value is returned.
If format is empty, return NULL.
If format already has the specified format, the return value is VARCHAR.

Description: The format string can contain literal characters and format specifiers starting with %. The literal characters in format must match the literal characters in str. The format specifier in format must match the date or time part in str.

Example

mysql> SELECT TO_DATE('2022-01-06 10:20:30','%Y-%m-%d %H:%i:%s') as result;
+--------------------------+
| result |
+--------------------------+
| 2022-01-06 10:20:30 |
+--------------------------+
1 row in set (0.00 sec)

limit

Currently, the date format only supports the data formats of yyyy-mm-dd and yyyymmdd.