Arithmetic overflow on column sum in sql server - sql-server

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.

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%'

Related

Integer function in snowsql

I am trying to run this query in snowflakes, but I keep having the error below. Can someone please tell me what I am doing wrong. I am trying to get the integer value for the ages.
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' And '5/31/2020')
AND (as_integer((Target_quikvalf.MAGE) / 5)*5))
Error Message:
SQL compilation error: invalid type [FLOAT] for parameter 'AS_INTEGER(variantValue...)'
The placement of where you are applying the function is the issue. Here we are converting the data type before applying the mathematical calculations.
You'll need to cast the FLOAT to INT can cast it like so:
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' AND '5/31/2020')
AND ((cast(Target_quikvalf.MAGE as int) / 5) * 5);
As an alternative, we can try converting the FLOAT to a number using TO_NUMBER. If Target_quikvalf.MAGE column was a variant data type, then you'd be able to run it like so (in the example below, we are forcing the converted float number to not have a tenths place and can support ages below 1000 since people can be over 100 years old):
SELECT DISTINCT Target_quikvalf.MPOLICY
FROM Target_quikvalf
WHERE (((Target_quikvalf.MPOLICY) Like '%N')
AND ("DATE" (Target_quikvalf.MISSUE) Between '5/1/2020' AND '5/31/2020')
AND ((to_number(Target_quikvalf.MAGE, 3, 0) / 5) * 5);
SELECT as_integer(5.1);
generates
SQL compilation error: invalid type [NUMBER(2,1)] for parameter 'AS_INTEGER(variantValue...)'
so don't use a function that is intended to "parsing variant data" to truncate you number, instead
cast
SELECT 5.1::int;
gives:
5.1::INT
5
given you are not getting the value scaled as you expect, let break it down:
SELECT 68 as age
, age/5 as float_scale
, float_scale::int as cast_age
, round(float_scale,0) as round_age
, trunc(float_scale,0) as trunc_age
, trunc_age * 5 as scaled_back;
gives:
AGE FLOAT_SCALE CAST_AGE ROUND_AGE TRUNC_AGE SCALED_BACK
68 13.600000 14 14 13 65
casting, and rounding appear to not be what you want, thus TRUNC is the key here
thus your code should be
(TRUNC(Target_quikvalf.MAGE / 5) *5)

SQL Scalar-Valued Function

I am looking to retrieve a value of the profit (FilmBoxOfficeDollar - FilmBudgetDollars) based on the Studio given as a parameter to the function.
USE Movies;
GO
CREATE FUNCTION fnmovieProfits(#StudioName nvarchar(255))
RETURNS int
AS
BEGIN
RETURN (SELECT SUM(FilmBoxOfficeDollars - FilmBudgetDollars)
FROM Film JOIN Studio
ON Film.FilmStudioID = Studio.StudioID
WHERE StudioName = #StudioName);
END;
GO
SELECT [dbo].[fnmovieProfits]('Dreamworks');
Whenever I run this through to pull the piece of data I get the following error:
Msg 8115, Level 16, State 2, Line 13
Arithmetic overflow error converting expression to data type int.
Any help would be much appreciated!
The problem you are experiencing is that you are overflowing the allowed value of a 32 bit number (INT); if you cast/convert to a 64 bit number (BIGINT) and return that datatype, the issue will be corrected. Proof of concept showing the issue:
DECLARE #BigNumber INT=2000000000
select CONVERT(BIGINT,#BigNumber) + CONVERT(BIGINT,#BigNumber) --returns 4,000,000,000
select (#BigNumber + #BigNumber) --errors with "Arithmetic overflow error converting expression to data type int."
BUT, do yourself a favor and use a view instead. Scalars like that are terrible for performance in reports. Scalar functions should never be used unless they are simply doing calculations based on input values (i.e. not hitting underlying, persisted data).
CREATE VIEW dbo.v_StudioProfits
AS
SELECT
StudioName,
SUM(CONVERT(BIGINT,FilmBoxOfficeDollars) - CONVERT(BIGINT,FilmBudgetDollars)) AS [Profit]
FROM Film
INNER JOIN Studio ON Film.FilmStudioID = Studio.StudioID
GROUP BY StudioName
GO
SELECT * FROM dbo.v_StudioProfits WHERE StudioName='Dreamworks'
Relevant reading on SQL Server datatypes. Specifically, integer datatypes.
Your sum is exceeding int range. You should define your return type as bigint:
CREATE FUNCTION fnmovieProfits(#StudioName nvarchar(255))
RETURNS bigint
AS.......
The maximum value you can return with int as a return type is 2147483647. Your sum is probably bigger than that.
One example of function that exceeds its return type:
CREATE FUNCTION testFunction()
RETURNS int
AS
BEGIN
RETURN (SELECT 2147483647 + 1);
END;
GO
SELECT [dbo].[testFunction]();
If you execute it you will get the following error:
Msg 8115, Level 16, State 2, Line 8
Arithmetic overflow error converting expression to data type int.
So the solution is just to increase your return type range by replacing int with bigint.

Arithmetic overflow error for select 2678400*1393 in SQL

select 2678400 * 1393
When I run this query, I'm getting this error in SQL Server:
Msg 8115, Level 16, State 2, Line 23
Arithmetic overflow error converting expression to data type int.
Please help me to resolve this issue.
Since this result overflows the capacity of INT - use BIGINT instead:
SELECT CAST(2678400 AS BIGINT) * 1393

Unable to perform arithmetic operation in select statement in specific scenario

While I was doing some multiplication in select statements, I found a special case where SQL Server is throwing an arithmetic overflow error.
When I executed the same in W3Schools SQL window, it worked. Below is my query where it is throwing error. I tried multiple permutations and combinations but it failed in most of the cases
SELECT 20000000 * 130
Msg 8115, Level 16, State 2, Line 4
Arithmetic overflow error converting expression to data type int.
Message window:
Output window:
Version information:
type int isn't enought, try float.
SELECT cast(20000000 as float) * cast(130 as float)
The result is the data type of the argument with the higher precedence. But if you have 2 int you can't get a float without casting
https://learn.microsoft.com/it-it/sql/t-sql/language-elements/multiply-transact-sql?view=sql-server-2017
I had used float but bigint is the same
SELECT CAST(20000000 AS BIGINT) * 130

How to make the power() function use decimal instead of integer?

The following query throws an error:
SELECT POWER( 10, 13)
Error message:
Msg 232, Level 16, State 3, Line 1
Arithmetic overflow error for type int, value = 10000000000.000000.
Whan can I do to prevent the int overflow?
Add a decimal place:
SELECT POWER( 10.0, 11 - LEN(9)) - 1
That causes an implicit cast from int to DECIMAL(18,6). You could write it for identical results as:
SELECT POWER(CAST(10 as DECIMAL(18,6)), 11 - LEN(9)) - 1
The POWER function return type is documented as:
Returns the same type as submitted in float_expression. For example,
if a decimal(2,0) is submitted as float_expression, the result
returned is decimal(2,0)
This means that your result is a DECIMAL(18,6), which in your case is big enough to hold the final result.
Use following
SELECT POWER( 10.0, 11 - LEN(9)) - 1
Explanation
Arithmetic overflow occurs when a calculation produces a result that falls outside the min & max values that a datatype can store. In your case, POWER( 10, 11 - LEN(9)) produces 10000000000, and the datatype is implicitly implemented as int. This value is greater than the max value that int can take, i.e., 2147483647, thus creating the overflow. If you use POWER( 10.0, 11 - LEN(9)) with 10.0, the calculation produces decimal, as decimal has higher precedence in SQL Server than int. Decimal has a range of - 10^38 +1 through 10^38 - 1, and so does not create overflow for your calculation.
You could use CONVERT:
SELECT CONVERT(DECIMAL(18,2), 10)
This specifies 2 decimal places, but you can modify the numbers within the brackets (18,5) to adjust this precision & scale as desired

Resources