STR_TO_DATE
Declaration
STR_TO_DATE(str, format)
Description
Use format to convert str to a DATETIME value, DATE value, or TIME value. The return value type depends on which parts of the date and time are included in format.
-
If
strorformatisNULL, the function returnsNULL. -
If
stris not in a valid date format, the return value varies based on the setting ofsql_mode:-
If
sql_modecontainsNO_ZERO_IN_DATE, the function returnsNULL. -
If
sql_modedoes not containNO_ZERO_IN_DATE:-
If
stris of the INT type andstr< 0, the function returns0000-00-00 00:00:00and generates a warning.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 warning (0.001 sec) -
If
stris of the INT type and 0 ≤str≤ 69, the function returnsNULLand generates a warning.SELECT STR_TO_DATE(0,'%Y-%b-%D %r');
+------------------------------+
| STR_TO_DATE(0,'%Y-%b-%D %r') |
+------------------------------+
| NULL |
+------------------------------+
1 row in set, 1 warning (0.000 sec) -
If
stris of the INT type and 69 <str≤ 99, the function returnsNULLand generates a warning.SELECT STR_TO_DATE(88,'%Y-%b-%D %r');
+-------------------------------+
| STR_TO_DATE(88,'%Y-%b-%D %r') |
+-------------------------------+
| NULL |
+-------------------------------+
1 row in set, 1 warning (0.000 sec) -
If
stris of the INT type and 100 <str≤ 9999, the function returnsNULLand generates a warning.SELECT STR_TO_DATE(2088,'%Y-%b-%D %r');
+---------------------------------+
| STR_TO_DATE(2088,'%Y-%b-%D %r') |
+---------------------------------+
| NULL |
+---------------------------------+
1 row in set, 1 warning (0.000 sec) -
If
stris of the INT type andstr> 10000, the function returnsNULLand generates a warning.SELECT STR_TO_DATE(10001,'%Y-%b-%D %r');
+----------------------------------+
| STR_TO_DATE(10001,'%Y-%b-%D %r') |
+----------------------------------+
| NULL |
+----------------------------------+
1 row in set, 1 warning (0.000 sec) -
If
stris a string, the function attempts to convert the string to a date and time and returns1999-11-30 00:00:00plus the value ofstras a date and time. If the conversion fails, the function returnsNULL.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 (0.001 sec)
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 set (0.001 sec) -
If
stris of another type, the function returnsNULLand generates a warning.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 (0.001 sec)
SELECT STR_TO_DATE(pi(),'%Y-%b-%D %r');
+---------------------------------+
| STR_TO_DATE(pi(),'%Y-%b-%D %r') |
+---------------------------------+
| NULL |
+---------------------------------+
1 row in set, 1 warning (0.001 sec)
-
-
For more information about the supported formats of format, see DATE_FORMAT.
Examples
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 (0.000 sec)