Fix Error Msg 8115 “Arithmetic overflow error converting expression to data type…” in SQL Server

If you’re getting error msg 8115 that includes the message Arithmetic overflow error converting expression to data type…, it’s probably because you’re trying to convert a value to a data type that can’t handle that value. For example, trying to convert a number to a tinyint but the number’s too big to fit into a tinyint.

To fix this issue, make sure you convert the value to a data type that can handle the size of the value that you’re trying to convert.

Example of Error

Here’s an example of code that results in the error:

DECLARE @bank_balance smallmoney = -20;
SELECT CAST( @bank_balance AS tinyint );

Result:

Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type tinyint.

This error occurred because I tried to convert a smallmoney value of -20 to a tinyint type. The tinyint type can only handle values in the range 0 to 255. The value I passed is outside of that range, and so we got the error.

We get the same error when doing an implicit conversion:

DECLARE @bank_balance smallmoney = -20;
DECLARE @wallet tinyint = @bank_balance;

Result:

Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type tinyint.

Solution

To fix the issue, we should convert to a data type that can handle the number. In our case, we can convert to a smallint type:

DECLARE @bank_balance smallmoney = -20;
SELECT CAST( @bank_balance AS smallint );

Result:

-20

And for the implicit conversion:

DECLARE @bank_balance smallmoney = -20;
DECLARE @wallet smallint = @bank_balance;

Result:

Commands completed successfully.

We can verify the type with the sys.dm_exec_describe_first_result_set function:

SELECT system_type_name
FROM sys.dm_exec_describe_first_result_set(
    'DECLARE @bank_balance smallmoney = -20;
SELECT CAST( @bank_balance AS smallint )', 
    null, 
    0);

Result:

smallint