Skip to content

Instantly share code, notes, and snippets.

@reanim8ed
Created November 30, 2023 08:59
Show Gist options
  • Save reanim8ed/3ef4fb6be844c01853255695704d9bb6 to your computer and use it in GitHub Desktop.
Save reanim8ed/3ef4fb6be844c01853255695704d9bb6 to your computer and use it in GitHub Desktop.
[Allow to set 0000 date in mysql] #mysql

If you absolutely need to set it to zeros and are aware of the implications and potential issues that might arise, then you can temporarily disable strict mode for your session, run the update statement, and then re-enable strict mode. Here's how you can do it:

  1. Disable Strict Mode Temporarily:

    SET @@session.sql_mode = REPLACE(@@session.sql_mode, 'NO_ZERO_DATE', '');
    SET @@session.sql_mode = REPLACE(@@session.sql_mode, 'STRICT_TRANS_TABLES', '');
  2. Run Your Update:

    UPDATE ps_specific_price
    SET `from` = '0000-00-00 00:00:00'
    WHERE `from` is null;
  3. Re-enable Strict Mode (if it was originally enabled):

    SET @@session.sql_mode = CONCAT(@@session.sql_mode, ',NO_ZERO_DATE,STRICT_TRANS_TABLES');

IMPORTANT: This method temporarily disables strict mode only for your current session and doesn't affect other connections. Always be cautious about changing SQL modes, even temporarily, especially on production databases. Make sure to take a backup before making changes.

However, keep in mind that the '0000-00-00 00:00:00' date might cause issues in some applications, libraries, or future versions of MySQL. It's often better to use a more universally recognized default date (like '1970-01-01 00:00:00'), or to allow NULLs in the column and use NULL as the default. If you must use zeros, just be aware of the potential implications and design your application logic accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment