0

how can I disable automatic conversion of date/datetime columns of invalid date to 0000-00-00? In my my.cnf I insert this line:

sql-mode = STRICT_ALL_TABLES,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE

in sections:

[client]
[mysqld]

Mysql server run on Debian server, and Mysql version is 5.1.63-0+squeeze1.

When I execute INSERT or UPDATE with PHP, invalid values are converted in 0000-00-00.

Thank you

EDIT:

table create:

CREATE TABLE IF NOT EXISTS `versioni` (
  `cod_versione` int(11) NOT NULL,
  `data` datetime NOT NULL,
  PRIMARY KEY (`cod_versione`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert statement that raise error in customer DB, otherwise in development are converted in '0000-00-00':

INSERT INTO versioni (cod_versione, data) VALUES (67, '2014-04-101 00:00:00');

11
  • 1
    Are you asking for MySQL to raise an error when you try to insert an invalid date, or to allow the invalid date to be entered in to the database?
    – MatBailie
    Apr 17, 2014 at 10:09
  • Have you restarted MySQL after you made changes to my.cnf?
    – fancyPants
    Apr 17, 2014 at 10:10
  • I would like MySQL to raise an error, actually mysql convert invalid dates.
    – webs86
    Apr 17, 2014 at 10:11
  • @fancyPants Yes I have restarted MySQL service with /etc/init.d/mysql restart command
    – webs86
    Apr 17, 2014 at 10:13
  • And if you do show variables like 'sql_mode'; what is its output?
    – fancyPants
    Apr 17, 2014 at 10:15

1 Answer 1

-1

Well, you could just try not inserting invalid dates...

But if you must, you should use a CHAR(10) column instead of a DATE column, as this will give you the... freedom you seek.


Alternatively, just use PHP functions, such as checkdate, to validate input before throwing it at the database ;)

12
  • I would errors to prevent errors during update of db of our customers
    – webs86
    Apr 17, 2014 at 10:08
  • He said he wanted it to raise an error. Char(10) will just allow you to save the string even if it is an invalid date (as long as it is the right length)
    – Adam
    Apr 17, 2014 at 10:13
  • @Adam When did OP say he wanted it to raise an error? Now, when did I post my answer? Okay then. Apr 17, 2014 at 10:13
  • @Niet: yeah, wasn't meaning to "blame" you in that sense. Just pointing out the answer doesn't fity the (updated) requirements
    – Adam
    Apr 17, 2014 at 10:15
  • 1
    Then surely you can parse for... wait, why does your .sql file even have invalid dates in the first place? Apr 17, 2014 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.