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.
You need to increase the width of the variable to store this number e.g. making @sample NUMERIC (6,2) will solve this error.
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.
"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.
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%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With