How will addition, subtraction, multiplication, and division operator work with date represented as MySQL string?

MySQLMySQLi Database

Such kind of calculations can cause unpredictable result because when the date is represented as MySQL string then MySQL tries to perform numeric operations on a string by taking only the first that appears. Following examples will clarify it −

mysql> select '2017-10-17' + 20;
+-------------------+
| '2017-10-17' + 20 |
+-------------------+
|      2037         |
+-------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select '2017-10-25' - 17;
+-------------------+
| '2017-10-25' - 17 |
+-------------------+
|              2000 |
+-------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select '2017-10-17' * 2;
+-------------------+
| '2017-10-17' * 20 |
+-------------------+
|              4034 |
+-------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select '2017-05-25'/5;
+----------------+
| '2017-05-25'/5 |
+----------------+
|          403.4 |
+----------------+
1 row in set, 1 warning (0.00 sec)
raja
Updated on 28-Jan-2020 10:40:17

Advertisements