Details
-
Bug
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.4(EOL)
Description
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00
- 2001-00-01
- 2001-01-00
- 2001-02-30
Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context.
However, this conversion works differently in different versions.
- In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values
- 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does.
It's not clear which way is correct.
Details:
In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
DATE('0000-00-00') AS d, |
DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, |
TIMESTAMP('0000-00-00 00:00:00') AS dt, |
TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; |
+------+------+------+-------+
|
| d | cmpd | dt | cmpdd |
|
+------+------+------+-------+
|
| NULL | 1 | NULL | 1 |
|
+------+------+------+-------+
|
Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser:
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT DATE'0000-00-00' < DATE'2001-01-01'; |
ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00'
|
At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates:
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
'0000-00-00' < DATE'2001-01-01' AS c1, |
'0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; |
+----+----+
|
| c1 | c2 |
|
+----+----+
|
| 1 | 1 |
|
+----+----+
|
String literals and functions DATE() and TIMESTAMP() work the same way.
In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
DATE('0000-00-00') < DATE'2001-01-01' AS c1, |
TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; |
+------+------+
|
| c1 | c2 |
|
+------+------+
|
| NULL | 1 |
|
+------+------+
|
Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero.
In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
DATE('0000-00-00') < DATE'2001-01-01' AS c1, |
TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 |
+------+------+
|
| c1 | c2 |
|
+------+------+
|
| 1 | 1 |
|
+------+------+
|
Conclusions
During a Slack discussion between serg and bar, the following decision was made:
Functions:
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
DATE('0000-00-00') AS d, |
DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, |
TIMESTAMP('0000-00-00 00:00:00') AS dt, |
TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd;``` |
should return NULL in all columns. A result of a function, DATE() and DATETIME() in this example, should not depend on its context. So it should work as follows:
- Functions values are calculated normally, NULL is returned.
- NULLs is passed into comparison operators.
- Comparison operators return NULL.
Literal:
SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; |
SELECT
|
'0000-00-00' < DATE'2001-01-01' AS cmpd, |
'0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; |
will still return non-NULL values. In this query we convert string literals '0000-00-00' to DATE and DATETIME for comparison.
Tests
Tests, in addition to functions DATE() and TIMESTAMP(), should also conver:
- CAST(AS DATE)
- CAST(AS DATETIME(N)).
Attachments
Issue Links
- relates to
-
MDEV-16991 Rounding vs truncation for TIME, DATETIME, TIMESTAMP
-
- Closed
-
-
MDEV-17318 CAST(LEAST(zero_date,non_zero_date) AS numeric_data_type) returns a wrong result
-
- Closed
-
-
MDEV-17335 COALESCE() respects NO_ZERO_DATE, NO_ZERO_IN_DATE but ignores unset ALLOW_INVALID_DATES
-
- Open
-
Activity
Link |
This issue relates to |
Link |
This issue relates to |
Description |
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context.
{code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} Looks fine so far. |
This problem is reproducible in 10.4 only.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} Looks fine so far. |
Labels | regression |
Description |
This problem is reproducible in 10.4 only.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} Looks fine so far. |
This problem with the DATE data type is reproducible in 10.4 only.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context, accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
This problem with the DATE data type is reproducible in 10.4 only. Earlier versions worked fine. However, the problem with DATETIME also presets in earlier version.
With NO_ZERO_DATE set, the function DATE() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00'),DATE('0000-00-00') < DATE'2001-01-01'; {code} {noformat} +--------------------+---------------------------------------+ | DATE('0000-00-00') | DATE('0000-00-00') < DATE'2001-01-01' | +--------------------+---------------------------------------+ | NULL | 1 | +--------------------+---------------------------------------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second column should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for the function TIMESTAMP(): {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable at least since 10.0, but only for the function TIMESTAMP(): {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Summary | NULL date value is erroneously converted to zero date in comparison context | Inconsistency of NULL date value conversion to zero date in comparison context |
Description |
Functions DATE() and TIMESTAMP() erroneously convert NULL to zero date.
- In 5.5 and 10.4 the problem happens with both functions DATE() and TIMESTAMP() - 10.0, 10.1, 10.2, 10.3 the problem is repeatable for TIMESTAMP(), while DATE() works fine h3. 10.4 returns a bad result for both DATE() and TIMESTAMP() With NO_ZERO_DATE set, functions DATE() and TIMESTAMP() returns NULL when not in comparison context, but not-null in comparison context. {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Looks wrong. The second and the fourth columns should also return NULL. MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as: - 2001-00-01 - 2001-01-00 - 2001-02-30 But converting NULL to non-NULL looks wrong. Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h2. The same problem is repeatable in version starting from 10.0 and up to 10.3, but only for DATETIME: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} The value for c2 is wrong. It should be NULL. Notice, DATE works fine in these versions. h2. The same problem is repeatable in 5.5, for both DATE and DATETIME {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are not accepted: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, for both DATE and DATETIME convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
Link |
This issue blocks |
Link | This issue relates to MDEV-17335 [ MDEV-17335 ] |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} h3. Conclusions During a Slack discussion between Sergei and Bar, the following desicion was made: h4. Functions: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd;``` {code} should return NULL in all columns. A result of a function, DATE() and DATETIME() in this example, should not depend on its context. So it should work as follows: - Functions values are calculated normally, NULL is returned. - NULLs is passed into comparison operators. - Comparison operators return NULL. h4. Literal: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS cmpd, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} will still return non-NULL values. In this query we convert string literals '0000-00-00' to DATE and DATETIME for comparison. h4. Tests Tests, in addition to functions DATE() and TIMESTAMP(), should also conver: - CAST(AS DATE) - CAST(AS DATETIME(N)). |
Description |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} h3. Conclusions During a Slack discussion between Sergei and Bar, the following desicion was made: h4. Functions: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd;``` {code} should return NULL in all columns. A result of a function, DATE() and DATETIME() in this example, should not depend on its context. So it should work as follows: - Functions values are calculated normally, NULL is returned. - NULLs is passed into comparison operators. - Comparison operators return NULL. h4. Literal: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS cmpd, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} will still return non-NULL values. In this query we convert string literals '0000-00-00' to DATE and DATETIME for comparison. h4. Tests Tests, in addition to functions DATE() and TIMESTAMP(), should also conver: - CAST(AS DATE) - CAST(AS DATETIME(N)). |
MariaDB performs loose date validation in comparison context (even with NO_ZERO_DATE, NO_ZERO_IN_DATE and without INVALID_DATES set in the current @@sql_mode), accepting wrong dates such as:
- 0000-00-00 - 2001-00-01 - 2001-01-00 - 2001-02-30 Functions DATE() and TIMESTAMP() convert invalid dates to zero date (instead of NULL) in comparison context. However, this conversion works differently in different versions. - In 5.5 and 10.4 both functions DATE() and TIMESTAMP() convert invalid dates to NULL, so comparison operators return non-NULL values - 10.0, 10.1, 10.2, 10.3, DATE() does not convert invalid dates to zero dates, while the function TIMESTAMP() does. It's not clear which way is correct. Details: h3. In 10.4 both DATE() and TIMESTAMP() convert invalid dates to zero dates (rather than NULL) for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} {noformat} +------+------+------+-------+ | d | cmpd | dt | cmpdd | +------+------+------+-------+ | NULL | 1 | NULL | 1 | +------+------+------+-------+ {noformat} Just for information: with NO_ZERO_DATE set, zero date literals are rejected by the parser: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE'0000-00-00' < DATE'2001-01-01'; {code} {noformat} ERROR 1525 (HY000): Incorrect DATE value: '0000-00-00' {noformat} At the same time, comparison between an invalid-date-in-string converts invalid dates to zero dates: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS c1, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +----+----+ | c1 | c2 | +----+----+ | 1 | 1 | +----+----+ {noformat} String literals and functions DATE() and TIMESTAMP() work the same way. h3. In version starting from 10.0 and up to 10.3 only TIMESTAMP() converts invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2; {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | NULL | 1 | +------+------+ {noformat} Notice, DATE() converted an invalid date to NULL, TIMESTAMP() converted an invalid datetime value to zero. h3. In 5.5, both DATE() and DATETIME() convert invalid dates to zero dates for comparison {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') < DATE'2001-01-01' AS c1, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS c2 {code} {noformat} +------+------+ | c1 | c2 | +------+------+ | 1 | 1 | +------+------+ {noformat} h3. Conclusions During a Slack discussion between [~serg] and [~bar], the following decision was made: h4. Functions: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT DATE('0000-00-00') AS d, DATE('0000-00-00') < DATE'2001-01-01' AS cmpd, TIMESTAMP('0000-00-00 00:00:00') AS dt, TIMESTAMP('0000-00-00 00:00:00') < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd;``` {code} should return NULL in all columns. A result of a function, DATE() and DATETIME() in this example, should not depend on its context. So it should work as follows: - Functions values are calculated normally, NULL is returned. - NULLs is passed into comparison operators. - Comparison operators return NULL. h4. Literal: {code:sql} SET sql_mode='NO_ZERO_DATE,NO_ZERO_IN_DATE'; SELECT '0000-00-00' < DATE'2001-01-01' AS cmpd, '0000-00-00 00:00:00' < TIMESTAMP'2001-01-01 00:00:00' AS cmpdd; {code} will still return non-NULL values. In this query we convert string literals '0000-00-00' to DATE and DATETIME for comparison. h4. Tests Tests, in addition to functions DATE() and TIMESTAMP(), should also conver: - CAST(AS DATE) - CAST(AS DATETIME(N)). |
Link |
This issue blocks |
Epic Link | MDEV-21071 [ 80504 ] |
Workflow | MariaDB v3 [ 89825 ] | MariaDB v4 [ 140950 ] |
A related problem is reported in MDEV-17335.