mySQL str_to_date() function returns error

前端 未结 3 451
情书的邮戳
情书的邮戳 2021-01-13 14:30

I keep receiving an error message when trying to convert a column, CreatedDate, of string date values in my Estimates table into the mySQL date format using str_to_date(). M

3条回答
  •  感情败类
    2021-01-13 15:13

    Disable NO_ZERO_DATE SQL mode:

    set @old_sql_mode = @@sql_mode; 
    set sql_mode = ''; 
    

    Run your statement:

    UPDATE Estimates
    SET CreatedDate = NULLIF(str_to_date(CreatedDate, '%c/%e/%y'), FROM_DAYS(0))
    

    Then enable original SQL modes:

    set sql_mode = @old_sql_mode;
    

    Disabling NO_ZERO_DATE mode will make STR_TO_DATE return zero date 0000-00-00 for invalid date strings, the same value is returned by FROM_DAYS(0). So NULLIF will convert zero dates to NULL.

    This answer was helpful.

提交回复
热议问题