Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic overflow error converting numeric to data type numeric

I am facing an error on my SQL script:

Arithmetic overflow error converting numeric to data type numeric

select x.MemberName,x.DOB,x.FilePath,x.Medication,x.NDC,x.Directions,x.Name,x.Strength,x.GenericName,x.QtyOrdered,x.DaysSupply,x.DateFilled, 
CASE
    WHEN x.test = 0  THEN 'N/A'
    WHEN compliance > 100.0   THEN '100.0'
    ELSE CONVERT(VARCHAR(5), CAST(FLOOR(compliance *10)/10.0 AS DECIMAL(3,1)))
END as [Compliance]

I am facing the error on just above syntax line.

like image 446
goofyui Avatar asked Aug 29 '11 14:08

goofyui


People also ask

How do you solve arithmetic overflow error converting numeric to data type numeric?

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

What does arithmetic overflow error convert to data type int mean?

The error "Arithmetic overflow error converting IDENTITY to data type int" comes when the IDENTITY value is inserted into a column of data type int, but the value is out-of-range.

What type of error is an arithmetic overflow?

An arithmetic overflow occurs when the output of an operation is a value bigger than can be stored in the destination data type. This means the data type must be numerically bound -- Integers, Floats, (maybe decimal). Strings and chars have different error message. This error message may also apply to date / datetime.

How can we avoid arithmetic overflow in SQL?

The solution to avoid this arithmetic overflow error is to change the data type from INT to BIGINT or DECIMAL(11,0) for example.


2 Answers

Here's your problem:

declare @compliance decimal(10,5)

set @compliance = 100.0  --  <----------------

select CAST(FLOOR(@compliance *10)/10.0 AS DECIMAL(3,1))

Throws "Arithmetic overflow error converting numeric to data type numeric" error. Changing to DECIMAL(4,1) works, or as @paola suggests, change your condition to >= 100.0

decimal(p,s): 

p (precision) is the maximum total number of decimal digits that will be stored, both to the left and to the right of the decimal point. The precision must be a value from 1 through the maximum precision of 38. The default precision is 18.

s (scale) is the number of decimal digits that will be stored to the right of the decimal point. This number is subtracted from p to determine the maximum number of digits to the left of the decimal point.

In your case decimal(3, 1) means a total of 3 digits with 1 digit to the right of the decimal point,

99.9

whereas decimal(4,1) provides a total of 4 digits with 1 digit to the right of the decimal point,

999.9
like image 95
Mitch Wheat Avatar answered Sep 28 '22 06:09

Mitch Wheat


This questions has already been answered, but the why is important.

Numeric defines the TOTAL number of digits, and then the number after the decimal.

So DECIMAL(4,1) shows 123.4 DECIMAL(4,3) shows 1.234

In both cases you have a total of 4 digits. In one case you have 1 after the decimal, leaving 3 in front of the decimal. And vice versa.

like image 43
BClaydon Avatar answered Sep 28 '22 05:09

BClaydon