How will addition, subtraction, multiplication and division operator work with date values stored in MySQL table?

MySQLMySQLi Database

When we try to do such kind of operations with date values stored in the table then MySQL is assuming the date values as the number and perform the arithmetic.

Suppose we have a table named ‘example’ having a date value in ‘orderdate’ column then following arithmetic operation will clarify the above −

mysql> select * from example;
+------------+
| orderdate  |
+------------+
| 2017-05-25 |
+------------+
1 row in set (0.00 sec)

mysql> select orderdate+10 from example;
+--------------+
| orderdate+10 |
+--------------+
|     20170535 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate*10 from example;
+--------------+
| orderdate*10 |
+--------------+
|    201705250 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate-10 from example;
+--------------+
| orderdate-10 |
+--------------+
|     20170515 |
+--------------+
1 row in set (0.00 sec)

mysql> select orderdate/10 from example;
+--------------+
| orderdate/10 |
+--------------+
|    2017052.5 |
+--------------+
1 row in set (0.00 sec)
raja
Updated on 28-Jan-2020 11:01:22

Advertisements