Bug #22992 Problems evaluating "NOW() BETWEEN b AND c" when 'b' and 'c' are DATE
Submitted: 4 Oct 2006 19:20 Modified: 5 Oct 2006 7:23
Reporter: Patrick Stein Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:5.0.24 OS:MacOS (MacOSX)
Assigned to: CPU Architecture:Any

[4 Oct 2006 19:20] Patrick Stein
Description:
This may be related to bugs 19765 and 21103.

Given a table with DATE columns 'start' and 'end', it is reasonable to want to find all rows where the current time is BETWEEN 'start' and 'end'.  Unfortunately, this is erratic when trying to compare a TIMESTAMP to two DATE columns.

Query:
  SELECT `start`, `end` FROM `datecmptest` WHERE NOW() BETWEEN `start` AND `end`;

Actual results:
  Empty set (0.00 sec)

Expected results:
  +------------+------------+
  | start      | end        |
  +------------+------------+
  | 2006-09-04 | 2006-11-04 | 
  +------------+------------+
  1 row in set (0.00 sec)

Interestingly, it works if NOW() is replaced with any of the following:
   DATE(NOW())
   CURDATE()
   CONCAT( CURDATE(), ' ', CURTIME() )

But, does *not* work with:
   TIMESTAMP( CONCAT( CURDATE(), ' ', CURTIME() ) )

It doesn't work either for CURDATE() BETWEEN TIMESTAMP(`start`) AND TIMESTAMP(`end`).

How to repeat:
CREATE TABLE `datecmptest` ( `start` DATE NOT NULL, `end` DATE NOT NULL );
INSERT INTO `datecmptest` VALUES ( CURDATE() - INTERVAL 1 MONTH, CURDATE() + INTERVAL 1 MONTH );
SELECT `start`, `end` FROM `datecmptest` WHERE NOW() BETWEEN `start` AND `end`;
SELECT `start`, `end` FROM `datecmptest` WHERE CURDATE() BETWEEN TIMESTAMP(`start`) AND TIMESTAMP(`end`);

Suggested fix:
Either generating an error message when the types in a BETWEEN are not the same or casting all of the operands to DATETIME.
[5 Oct 2006 7:23] Sveta Smirnova
Thank you for the report.

I can not repeat it with current BK sources.