Bug #43317 addtime() returns different results depending on how the date is formatted
Submitted: 3 Mar 2009 0:31 Modified: 3 Apr 2009 6:54
Reporter: Bruce Rowe Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:5.0.45 and 5.0.51a OS:Linux
Assigned to: CPU Architecture:Any
Tags: adddtime, dates

[3 Mar 2009 0:31] Bruce Rowe
Description:
I've been working on the daily sales reports, by day with the time added. I have run into something unexpected. All the tables are Innodb.

I run the query:

    mysql> select sale_num, today_date, today_time from mainsale where addtime(today_date, today_time ) >= '2009-2-27 00:00:00' and addtime( today_date, today_time ) <= '2009-2-27 24:00:00' and clerk='webtix';
    +----------+------------+------------+
    | sale_num | today_date | today_time |
    +----------+------------+------------+
    |   108129 | 2009-02-27 | 19:52      |
    |   108366 | 2009-02-28 | 10:49      |<--
    |   108394 | 2009-02-28 | 00:36      |<--
    |   108481 | 2009-04-16 | 23:54      |<--
    |   109051 | 2009-02-27 | 19:37      |
    |   109865 | 2009-02-27 | 10:48      |
    |   109868 | 2009-02-27 | 10:45      |
    |   109879 | 2009-02-27 | 11:40      |
    |   109882 | 2009-02-27 | 10:37      |
    |   109974 | 2009-02-27 | 16:26      |
    |   109982 | 2009-02-27 | 16:25      |
    |   109986 | 2009-02-27 | 16:24      |
    |   110028 | 2009-02-27 | 19:44      |
    +----------+------------+------------+
    13 rows in set

This is wrong. Look at records 2, 2 & 4. They are for the wrong date. Now, I change the query slightly to:

    mysql> select sale_num, today_date, today_time from mainsale where addtime(today_date, today_time ) >= '2009-02-27 00:00:00' and addtime( today_date, today_time ) <= '2009-02-27 24:00:00' and clerk='webtix';
    +----------+------------+------------+
    | sale_num | today_date | today_time |
    +----------+------------+------------+
    |   108129 | 2009-02-27 | 19:52      |
    |   109051 | 2009-02-27 | 19:37      |
    |   109865 | 2009-02-27 | 10:48      |
    |   109868 | 2009-02-27 | 10:45      |
    |   109879 | 2009-02-27 | 11:40      |
    |   109882 | 2009-02-27 | 10:37      |
    |   109974 | 2009-02-27 | 16:26      |
    |   109982 | 2009-02-27 | 16:25      |
    |   109986 | 2009-02-27 | 16:24      |
    |   110028 | 2009-02-27 | 19:44      |
    +----------+------------+------------+
    10 rows in set

And, these query results are correct.

Have I found a bug in MySQL?

Oddly enough, it only seems to happen with the addtime() function. If I use this query, I get:

    mysql> select sale_num, today_date, today_time from mainsale where today_date >= '2009-2-27' and today_date <= '2009-2-27' and clerk='Webtix';
    +----------+------------+------------+
    | sale_num | today_date | today_time |
    +----------+------------+------------+
    |   109882 | 2009-02-27 | 10:37      |
    |   109868 | 2009-02-27 | 10:45      |
    |   109865 | 2009-02-27 | 10:48      |
    |   109879 | 2009-02-27 | 11:40      |
    |   109986 | 2009-02-27 | 16:24      |
    |   109982 | 2009-02-27 | 16:25      |
    |   109974 | 2009-02-27 | 16:26      |
    |   109051 | 2009-02-27 | 19:37      |
    |   110028 | 2009-02-27 | 19:44      |
    |   108129 | 2009-02-27 | 19:52      |
    +----------+------------+------------+
    10 rows in set

How to repeat:
Add leading zeros to the date in addtime() and then remove them

Suggested fix:
Have WySQL check for 2 digits in the month and day. Then, add the zero if necessary.
[3 Mar 2009 6:54] Valeriy Kravchuk
Thank you for the problem report. Please, try to repeat with a newer version, 5.0.77. In case of the same problem, please, send a dump of the problematic table or, at least, SHOW CREATE TABLE results for it.
[3 Apr 2009 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".