Home > Database > Mysql Tutorial > How can we ignore negative values ​​returned by MySQL DATEDIFF() function?

How can we ignore negative values ​​returned by MySQL DATEDIFF() function?

PHPz
Release: 2023-09-23 21:33:11
forward
1708 people have browsed it

我们如何忽略 MySQL DATEDIFF() 函数返回的负值?

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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template