Syntax
STR_TO_DATE(str, format)
Purpose
Converts the str value to a DATETIME, DATE, or TIME value by using the format parameter. The return value type depends on the parts of the date and time in the format parameter.
If
strorformatisNULL, the function returnsNULL.If
stris not in a valid date format, the return value depends on the setting ofsql_mode:If
sql_modecontainsNO_ZERO_IN_DATE, the function returnsNULL.If
sql_modedoes not containNO_ZERO_IN_DATE:If
stris an integer andstris less than 0, the function returns0000-00-00 00:00:00and issues a warning.obclient> SELECT STR_TO_DATE(-100,'%Y-%b-%D %r'); +---------------------------------+ | STR_TO_DATE(-100,'%Y-%b-%D %r') | +---------------------------------+ | 0000-00-00 00:00:00 | +---------------------------------+ 1 row in set, 1 warningIf
stris an integer and 0 ≤str≤ 69, the function returns1999-11-30 00:00:00plus the year specified bystrand issues a warning.obclient> SELECT STR_TO_DATE(0,'%Y-%b-%D %r'); +------------------------------+ | STR_TO_DATE(0,'%Y-%b-%D %r') | +------------------------------+ | 1999-11-30 00:00:00 | +------------------------------+ 1 row in setIf
stris an integer and 69 <str≤ 99, the function returns1969-11-30 00:00:00plus the year specified bystr- 70 and issues a warning.obclient> SELECT STR_TO_DATE(88,'%Y-%b-%D %r'); +-------------------------------+ | STR_TO_DATE(88,'%Y-%b-%D %r') | +-------------------------------+ | 1987-11-30 00:00:00 | +-------------------------------+ 1 row in setIf
stris an integer and 100 <str≤ 9999, the function returns0099-11-30 00:00:00plus the year specified bystr- 100 and issues a warning.obclient> SELECT STR_TO_DATE(2088,'%Y-%b-%D %r'); +---------------------------------+ | STR_TO_DATE(2088,'%Y-%b-%D %r') | +---------------------------------+ | 2087-11-30 00:00:00 | +---------------------------------+ 1 row in setIf
stris an integer andstris greater than 10000, the function returnsNULLand issues a warning.obclient> SELECT STR_TO_DATE(10001,'%Y-%b-%D %r'); +----------------------------------+ | STR_TO_DATE(10001,'%Y-%b-%D %r') | +----------------------------------+ | NULL | +----------------------------------+ 1 row in set, 1 warningIf
stris a string, the function attempts to convert the string to a date and time value and returns1999-11-30 00:00:00plus the date and time specified bystr. If the string cannot be converted to a date and time value, the function returnsNULL.obclient> SELECT STR_TO_DATE('a2014-Jan-1st 5:5:5 pm', '%Y-%b-%D %r'); +------------------------------------------------------+ | STR_TO_DATE('a2014-Jan-1st 5:5:5 pm', '%Y-%b-%D %r') | +------------------------------------------------------+ | NULL | +------------------------------------------------------+ 1 row in set, 1 warning obclient> SELECT STR_TO_DATE('a2014-Jan-1st 5:5:5 pm', 'a%Y-%b-%D %r'); +-------------------------------------------------------+ | STR_TO_DATE('a2014-Jan-1st 5:5:5 pm', 'a%Y-%b-%D %r') | +-------------------------------------------------------+ | 2014-01-01 05:05:05 | +-------------------------------------------------------+ 1 row in setIf
stris of another type, the function returnsNULLand issues a warning.obclient> SELECT STR_TO_DATE(1.6,'%Y-%b-%D %r'); +--------------------------------+ | STR_TO_DATE(1.6,'%Y-%b-%D %r') | +--------------------------------+ | NULL | +--------------------------------+ 1 row in set, 1 warning obclient> SELECT STR_TO_DATE(pi(),'%Y-%b-%D %r'); +---------------------------------+ | STR_TO_DATE(pi(),'%Y-%b-%D %r') | +---------------------------------+ | NULL | +---------------------------------+ 1 row in set, 1 warning
For information about the supported date and time formats, see DATE_FORMAT.
Examples
obclient> SELECT STR_TO_DATE('2014-Jan-1st 5:5:5 pm', '%Y-%b-%D %r');
+-----------------------------------------------------+
| STR_TO_DATE('2014-Jan-1st 5:5:5 pm', '%Y-%b-%D %r') |
+-----------------------------------------------------+
| 2014-01-01 05:05:05 |
+-----------------------------------------------------+
1 row in set
