How Can MySQL CAST handle overflow?


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)

Updated on: 30-Jan-2020

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements