Description:
If you quote a DATE without using delimiters (i.e., '20060301000000') and use it in the HAVING ... BETWEEN clause, it does not treat it as a datetime. If it is unquoted, then it works fine.
This also affects NOW() as well.
How to repeat:
CREATE TABLE t1 ( a DATETIME );
INSERT INTO t1 VALUES ('2006-03-01 01:02:03');
SELECT a FROM t1 WHERE a BETWEEN '20060301000000' AND '20060301235959';
- returns the row
SELECT a FROM t1 WHERE a GROUP BY a HAVING a BETWEEN '20060301000000' AND '20060301235959';
- returns the empty set
SELECT a FROM t1 WHERE a GROUP BY a HAVING a BETWEEN 20060301000000 AND 20060301235959;
- returns the row
------------------------------
A second example shows that this affects NOW() as well:
SET @a:= CURDATE();
SET @a:= REPLACE(CURDATE(),'-','');
SELECT @a;
- returns 20060301
SELECT NOW() BETWEEN CONCAT(@a,'000000') AND CONCAT(@a,'235959');
- returns 0 - false
To show that NOW() is also affected
SELECT NOW() BETWEEN CONCAT(@a,'000000')+0 AND CONCAT(@a,'235959')+0;
- returns 1 - true
Description: If you quote a DATE without using delimiters (i.e., '20060301000000') and use it in the HAVING ... BETWEEN clause, it does not treat it as a datetime. If it is unquoted, then it works fine. This also affects NOW() as well. How to repeat: CREATE TABLE t1 ( a DATETIME ); INSERT INTO t1 VALUES ('2006-03-01 01:02:03'); SELECT a FROM t1 WHERE a BETWEEN '20060301000000' AND '20060301235959'; - returns the row SELECT a FROM t1 WHERE a GROUP BY a HAVING a BETWEEN '20060301000000' AND '20060301235959'; - returns the empty set SELECT a FROM t1 WHERE a GROUP BY a HAVING a BETWEEN 20060301000000 AND 20060301235959; - returns the row ------------------------------ A second example shows that this affects NOW() as well: SET @a:= CURDATE(); SET @a:= REPLACE(CURDATE(),'-',''); SELECT @a; - returns 20060301 SELECT NOW() BETWEEN CONCAT(@a,'000000') AND CONCAT(@a,'235959'); - returns 0 - false To show that NOW() is also affected SELECT NOW() BETWEEN CONCAT(@a,'000000')+0 AND CONCAT(@a,'235959')+0; - returns 1 - true