As we all know, the DATEDIFF() function is used to get the number of days difference between two dates. Therefore, it is also likely to return negative values.
mysql> select * from differ; +------------+-------------+ | OrderDate | WorkingDate | +------------+-------------+ | 2017-10-22 | 2017-10-29 | | 2017-10-25 | 2017-10-30 | | 2017-10-25 | 2017-11-30 | +------------+-------------+ 3 rows in set (0.00 sec)
The above query will return the values in table "differ". Now, if someone wants to get the difference between OrderDate and workingDate, then the output will be negative as shown below -
mysql> Select DATEDIFF(OrderDate, WorkingDate)AS 'DIFFERENCE IN DAYS' from differ; +--------------------+ | DIFFERENCE IN DAYS | +--------------------+ | -7 | | -5 | | -36 | +--------------------+ 3 rows in set (0.00 sec)
But we can ignore these negative values using MySQL ABS() function as follows -
mysql> Select ABS(DATEDIFF(OrderDate, WorkingDate))AS 'DIFFERENCE IN DAYS' from differ; +--------------------+ | DIFFERENCE IN DAYS | +--------------------+ | 7 | | 5 | | 36 | +--------------------+ 3 rows in set (0.00 sec)
The above is the detailed content of How can we ignore negative values returned by MySQL DATEDIFF() function?. For more information, please follow other related articles on the PHP Chinese website!