How to fix error: Arithmetic overflow error converting expression to data type int - sql-server

I just used count function in query statement but got an error as shown below:
SELECT count(*)
FROM [my_table_data]
Error messages:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
Please help me to find the solution!

Try BIG_COUNT instead. I believe it's supported since SQL Server 2012
SELECT big_count(*) FROM [my_table_data]

Related

How can I know which value raise the error `Arithmetic overflow error converting numeric to data type numeric.`

I'm doing an ETL processes with some SQL Server procedures.
WITH CTE (qty_top, qty_lvl) AS
(
SELECT
CONVERT(decimal(20,6), t.unity),
CONVERT(decimal(20,6), t.unity)
FROM
top_tab
UNION ALL
SELECT
CONVERT(decimal(20,6), cte.qty * t.unity),
CONVERT(decimal(20,6), nom.Nomenclature_unite_production)
FROM
CTE_NOM cte
JOIN
tab t
)
This is my SQL Request (very simplified). When I do this, this raise me an Arithmetic overflow error converting numeric to data type numeric.
I tried to change my decimal(20,6) into a bigger number (30,6) but it still doesn't work.
How can I know which values raise me this error ? So It can help me to resolve this error.

Msg 8152, Level 16, State 14, String or binary data would be truncate

I have procedure which put records to table, specifically from field data type money to field data type numeric(11,0) by
insert into tab1(decimal_field_1)
select convert(numeric(11, 0), money_field_1)
from tab2
After execution I get information with warning:
Msg 8152, Level 16, State 14
String or binary data would be truncated
I consciously convert that column from money to decimal(11,0) and I don't want to use set ansi_warnings OFF.
So what wrong I do? How correctly convert that data?
That error is typically involved in truncating strings (e.g., varchar, nvarchar) rather than numbers (e.g., the string is too long to fit).
If I have a too-large money value converting to numeric(11,0), it gives me the error Msg 8115, Level 16, State 8, Line 5 Arithmetic overflow error converting money to data type numeric.
As such, the error is unlikely to be triggered by the CONVERT, but instead in the INSERT. You could test this by just doing a select convert(numeric(11, 0), money_field_1) from tab2 which should work OK - showing the CONVERT is fine.
Are you only inserting into 1 column decimal_field_1?
If so, are you sure that decimal_field_1 is also numeric(11,0) rather than a varchar or nvarchar?
If you are inserting other columns, check the other fields. It's likely the error is coming from there.
The money data type has been deprecated for a while. Have a look at this article about avoiding it.
"MONEY is accurate between -922,337,203,685,477.5808 (-922,337
trillion) and 922,337,203,685,477.5807 (922,337 trillion)"
Try using DECIMAL(19, 4)
insert into tab1(decimal_field_1)
select convert(numeric(19, 4), money_field_1)
from tab2;

select, avg statement not working

I have a table called Employee with about 17 different columns on it. One of those columns is called age. I am suppose to write up a statement that will get the average age of all the employees with an AS in the statement. Here is the statement I have written up and unfortunately I continue to get an error message. Also, I am new at this so please don't beat me to hard...lol.
SELECT AVG(Age) AS Average_Age FROM Employee
I get below error..
msg 8117, state 16, line 1 invalid syntax for operand avg.
I believe the message is telling me that the avg is trying to calculate, but cannot do it.
Why did I see this done on a youtube video and it work perfectly for the person who created it? While I did the same exact statement I got it wrong...can someone explain that?
Update as Per comments:
Error Message:
Operand data type varchar is invalid for avg operator.
If age is a varchar column you can try
SELECT AVG(cast(Age as int)) AS Average_Age FROM Employee
( but you may want to think about changing the datatype )

Cannot use modulus only in query window that connected to server

Currently I have a problem with SQL Server.
I just do a query like this :
declare #var2 int
set #var2 = ((7500.50 * 100) % 100)
select #var2
If I execute in local machine (query window connected to local), it returns 50.
But if I run that query in query window that is connected to my office server, it results in:
Msg 8117, Level 16, State 1, Line 2
Operand data type numeric is invalid for modulo operator.
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with void type
I've tried in both SQL Server 2005 and 2008.
Anyone ever face same problem or know the solution ?
Thanx a lot all.
Check the compatibility level of the server. It is likely at 80, which is for SQL Server 2000 compatibility. In SQL Server 2000, modulo only supported the INT data types. In SQL 2005 and up, other numeric data types are allowed and implicit conversions will happen, since in your formula you have both numeric and integer data types.
I found a script to modify compatibility level on all databases of a server with filtering of databases you wouldn't want to change.

TSQL, case, convert, replace end

Need help understanding what I am doing with the syntax here, please help! I not only need to convert from float to decimal but I also need to override the original data being migrated to the same format and data (in this case) where necessary to match the newer data.
,CASE
WHEN fInvReqMargin IS NOT NULL THEN
(CONVERT(DECIMAL(7, 7), fInvReqMargin)(REPLACE(fInvReqMargin, fInvReqMargin, INVESTOR_REQUIRED_MARGIN_FC)))
ELSE NULL
END as INVESTOR_REQUIRED_MARGIN_FC
error: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Thank you!
The problem is with the CONVERT and REPLACE syntax. Without seeing the data I can't determine if you're trying to supply the style parameter to CONVERT. If you are then the syntax should be
CONVERT( DECIMAL ( 7,7 ), fInvReqMargin,(REPLACE(fInvReqMargin, fInvReqMargin, INVESTOR_REQUIRED_MARGIN_FC)))

Resources