MySQL STR_TO_DATE NULL on error

前端 未结 2 1649
故里飘歌
故里飘歌 2020-12-21 07:39

I\'m currently migrating a table with dates in VARCHAR columns to a new table with DATE columns. I managed to sanitize the string values in the old

相关标签:
2条回答
  • 2020-12-21 07:58

    A possible workaround is to turn off strict mode, either for the whole server, for a particular session, or for just a few statements. For example:

     set @old_sql_mode = @@sql_mode; 
     set sql_mode = ''; 
     -- Run some statements which may result in error
     set sql_mode = @old_sql_mode;
    

    Additional Info

    MySQL Documentation

    0 讨论(0)
  • 2020-12-21 08:19

    In addition to @EduardoDennis answer use NULLIF to filter out zero dates:

    INSERT INTO newFancyTable (created_at)
    SELECT nullif(str_to_date(created, '%Y-%m-%d'), from_days(0)) FROM oldCrappyTable;
    

    See my full answer here.

    0 讨论(0)
提交回复
热议问题