Unable to perform arithmetic operation in select statement in specific scenario - sql-server

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

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)

On Insert: Arithmetic overflow error converting expression to data type int

I tried creating my third table of phone numbers, I wrote the following commands:
CREATE TABLE MYPHONE (EMPLOYEE_ID INT, PHONE_NUMBER INT)
INSERT INTO MYPHONE
VALUES (1 , 7894561230)
But when I tried executing this previous INSERT command, I got an error:
Msg 8115, Level 16, State 2, Line 46
Arithmetic overflow error converting expression to data type int.
An int probably isn't the best data type for a phone number - you don't need to do any arithmetic on phone numbers, so why use a numerical data type? Store it as a string (or varchar in SQL).
Also, if you did need to store a value as an int, 2,147,483,647 is the maximum - anything higher would "overflow", hence the error you're getting. A long (or bigint in SQL) would allow values up to 9,223,372,036,854,775,808.
Allowed range of INT datatype is -2,147,483,648 to 2,147,483,647
You can get more details on the allowed range here
You are trying to insert beyond that range. For phone number better choose VARCHAR data type. If you still want to go for number, in that case use BIGINT instead of INT.

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

Arithmetic overflow on column sum in 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%'

Resources