Skip to main content
Version: V1.0.0

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 str or format is NULL, the function returns NULL.

  • If str is not in a valid date format, the return value varies based on the setting of sql_mode:

    • If sql_mode contains NO_ZERO_IN_DATE, the function returns NULL.

    • If sql_mode does not contain NO_ZERO_IN_DATE:

      • If str is of the INT type and str < 0, the function returns 0000-00-00 00:00:00 and 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 str is of the INT type and 0 ≤ str ≤ 69, the function returns NULL and 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 str is of the INT type and 69 < str ≤ 99, the function returns NULL and 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 str is of the INT type and 100 < str ≤ 9999, the function returns NULL and 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 str is of the INT type and str > 10000, the function returns NULL and 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 str is a string, the function attempts to convert the string to a date and time and returns 1999-11-30 00:00:00 plus the value of str as a date and time. If the conversion fails, the function returns NULL.

        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 str is of another type, the function returns NULL and 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)