Bug #71322 | DATEDIFF Returns Erroneously In SELECT IF Query | ||
---|---|---|---|
Submitted: | 9 Jan 2014 5:21 | Modified: | 12 Nov 2019 22:40 |
Reporter: | Michael McNally | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S3 (Non-critical) |
Version: | 5.1.44-community, 5.1.73,5.5.35, 5.6.15, 5.7.3 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | DATEDIFF, IF |
[9 Jan 2014 5:21]
Michael McNally
[9 Jan 2014 11:12]
Valeriy Kravchuk
It seems that all versions are affected: mysql> SELECT -> TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW()) AS `A `, -> IF(FALSE, NOW(), TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW())) AS `B `; +----------+---------------------+ | A | B | +----------+---------------------+ | 01:00:00 | 2014-01-09 01:00:00 | +----------+---------------------+ 1 row in set (0.03 sec) mysql> SELECT -> TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW()) AS `A` , -> IF(FALSE, NULL, TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW())) AS `B` ; +----------+----------+ | A | B | +----------+----------+ | 01:00:00 | 01:00:00 | +----------+----------+ 1 row in set (0.00 sec) mysql> select version(); +------------+ | version() | +------------+ | 5.6.15-log | +------------+ 1 row in set (0.00 sec)
[9 Jan 2014 11:21]
MySQL Verification Team
Hello Michael, Thank you for the report. Verified as described. Thanks, Umesh
[12 Nov 2019 22:40]
Roy Lyseng
Posted by developer: This is not a bug. In the IF statement, NOW() returns type DATETIME, and TIMEDIFF returns type TIME. However, the returned type of the IF is the aggregated type of those two types, which is DATETIME. If a TIME return type is wanted from the IF, use the TIME() function with NOW() as argument to return a TIME value, like this: SELECT TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW()) AS `A`, IF(FALSE, TIME(NOW()), TIMEDIFF(DATE_ADD(NOW(), INTERVAL 1 HOUR), NOW())) AS `B`