Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic overflow on column sum in sql server

Tags:

I'm trying to get a column total but when i run this query i get the following error. Any advice?

SELECT SUM(Size) as total FROM  AllDocs Where DirName LIKE 'sites/test/test%'   ERROR: Msg 8115, Level 16, State 2, Line 1 Arithmetic overflow error converting expression to data type int. Warning: Null value is eliminated by an aggregate or other SET operation. 
like image 264
MG. Avatar asked Aug 03 '09 15:08

MG.


People also ask

How do you fix arithmetic overflow in SQL?

You need to increase the width of the variable to store this number e.g. making @sample NUMERIC (6,2) will solve this error.

How do you handle arithmetic overflow?

If you want to ensure that arithmetic operations will throw overflow exceptions if an overflow happens, you need to use the checked { ... } code block. When using the checked { ... } code block, if any arithmetic operation causes an overflow, an OverflowException will be thrown, and will need to be catched and handled.

What is arithmetic overflow error?

"Arithmetic overflow error converting IDENTITY to data type int" error means the value of IDENTITY is overflowing range of data type of that particular column.


1 Answers

While all your sizes can fit into INT (up to 2^31 - 1), their SUM cannot.

Cast them into BIGINT:

SELECT  SUM(CAST(Size AS BIGINT)) as total FROM    AllDocs WHERE   DirName LIKE 'sites/test/test%' 
like image 127
Quassnoi Avatar answered Oct 05 '22 11:10

Quassnoi