A zero date is treated like NULL in mysql and returned for an is null check.
CREATE TABLE t1 (
`id` int(11) NOT NULL AUTO_INCREMENT,
`enddate` datetime NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO t1(`enddate`) VALUES ('0000-00-00 00:00:00');
In mysql 5.7 both these queries return the row. In mysql 8.4 only the first query returns the row. This seems to be a failure in basic boolean logic of the OR operator. Am i missing something?
SELECT * FROM t1 WHERE enddate IS NULL;
SELECT * FROM t1 WHERE enddate IS NULL OR enddate > '2025-01-01 00:00:00';
A zero date is treated like NULL in mysql and returned for an is null check.
CREATE TABLE t1 (
`id` int(11) NOT NULL AUTO_INCREMENT,
`enddate` datetime NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO t1(`enddate`) VALUES ('0000-00-00 00:00:00');
In mysql 5.7 both these queries return the row. In mysql 8.4 only the first query returns the row. This seems to be a failure in basic boolean logic of the OR operator. Am i missing something?
SELECT * FROM t1 WHERE enddate IS NULL;
SELECT * FROM t1 WHERE enddate IS NULL OR enddate > '2025-01-01 00:00:00';
Share
Improve this question
asked Feb 16 at 22:46
dan carterdan carter
4,3612 gold badges35 silver badges36 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0To be able to reproduce your issue sql_mode NO_ZERO_DATE should be disabled. This allows inserting invalid dates.
In that case for the date
and datetime
datatype 0000-00-00
or 0000-00-00 00:00:00
are considered both NULL
and NOT NULL
from MySQL. Not for timestamp
datatype. though.
There is a good discussion on Is an invalid date considered the same as a NULL value? on this matter.
This seems to be a failure in basic boolean logic of the OR operator. Am i missing something?
SELECT * FROM t1 WHERE enddate IS NULL OR enddate > '2025-01-01 00:00:00';
enddate IS NULL
evaluates to TRUE
enddate > '2025-01-01 00:00:00'
evaluates to UNKNOWN
or FALSE
(My guess unknown)
Starting from MySQL 8+ TRUE OR UNKNOWN
evaluates to NULL OR FALSE, which means the row is excluded from the result set.
See example
INSERT
. – Barmar Commented Feb 16 at 22:530000-00-00
is notNULL
. And theNOT NULL
option in the column means it can never be null. – Barmar Commented Feb 16 at 22:54