How Can MySQL CAST handle overflow?

MySQLMySQLi Database

MySQL CAST can handle overflow occurs during numerical expression assessment. Suppose if numeric expression evaluation produces overflow then MySQL reflects an error message. Now to handle this overflow we can change that numeric value to UNSIGNED with the help of CAST.

For example on adding 1 to BIGINT maximum value, MySQL produce an error due to overflow as follows −

mysql> Select 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807+1)'

Now, with the help of CAST, MySQL handles this kind of overflow as follows:

mysql> Select CAST(9223372036854775807 AS UNSIGNED) +1;

| CAST(9223372036854775807 AS UNSIGNED) +1 |
| 9223372036854775808                      |
1 row in set (0.07 sec)
Published on 19-Feb-2018 13:42:12