SQL Server Error 8115: Arithmetic Overflow
Diagnose SQL Server Msg 8115 from integer expressions, SUM, AVG, COUNT, and conversions; widen values before the operation.
On this page
SQL Server Msg 8115 means a value or expression exceeded the range of its target data type. Read the complete message to identify that target: the fix for an overflowing int sum is different from the fix for a too-small decimal precision. Microsoft’s int and bigint ranges show that int ranges from -2,147,483,648 through 2,147,483,647.
Widen integer operands before arithmetic
This expression overflows because SQL Server evaluates the addition as int:
DECLARE @a int = 2000000000;
DECLARE @b int = 2000000000;
SELECT @a + @b;
Convert an operand to a wider type before the addition so the intermediate result is calculated as bigint:
SELECT CONVERT(bigint, @a) + @b AS total;
Converting only the final result is too late if the arithmetic expression has already overflowed. Use a data type whose range matches the largest possible intermediate and final values; see the SQL Server data type precedence reference.
Check aggregate return types
SUM(int_expression) returns int, so the total can overflow even when every individual row value fits in an int. Cast the expression before SUM to widen the aggregate’s return type:
SELECT SUM(CONVERT(bigint, Amount)) AS total_amount
FROM dbo.Sales;
Microsoft documents that SUM returns the type of its input category and that an int expression returns int; bigint input returns bigint. Use a decimal(p, s) input when the values require a fixed-point range or fractional scale, and choose its precision and scale for the expected total. See Microsoft’s SUM return types and the SQL Server decimal reference.
AVG(int_expression) also returns int and can overflow while summing its inputs, even when the final average would fit in an int:
DECLARE @values table (amount int);
INSERT INTO @values VALUES (2000000000), (2000000000);
SELECT AVG(amount) AS average_amount
FROM @values;
The two values fit in int, and their average is 2,000,000,000, but the intermediate sum exceeds the int limit. Convert the input to a suitably sized decimal before calling AVG() when fractional precision and a wider sum are required:
SELECT AVG(CONVERT(decimal(19, 4), amount)) AS average_amount
FROM @values;
Microsoft documents both the return type for AVG(int) and the error raised when its sum exceeds that type’s range in the AVG reference.
Use COUNT_BIG for large row counts
COUNT(*) returns int. If its result exceeds 2,147,483,647, SQL Server normally raises Msg 8115; when both ARITHABORT and ANSI_WARNINGS are OFF, COUNT returns NULL instead. Use COUNT_BIG(*) when a result might exceed the int limit; it returns bigint:
SELECT COUNT_BIG(*) AS row_count
FROM dbo.LargeTable;
See Microsoft’s COUNT overflow guidance and COUNT_BIG reference.
Distinguish overflow from invalid text
Msg 8115 indicates that the value or result is outside the target type’s range. Invalid text that cannot be parsed as an integer commonly raises Msg 245 instead; see SQL Server Error 245. If the full 8115 message names another type, check that type’s range, the expression’s intermediate type, and any aggregate return type before changing the schema.
Msg 8115 can also occur when ROUND() with a negative length needs more integer digits than the input’s decimal precision can hold.
Browse the SQL Server error troubleshooting index for other message guides.