5

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.

2
  • and what values does column compliance hold? Commented Aug 29, 2011 at 14:46
  • it has values like 'N/A','100.0' or '99.1' ... i.e) it has both float and nvarchar Commented Aug 29, 2011 at 14:48

2 Answers 2

9

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
Sign up to request clarification or add additional context in comments.

3 Comments

@Compliance has to accept both NVARCHAR and FLOAT. i.e) If there is no Compliance .. we need to set it as N/A
I'm not suggesting otherwise.
@Chok, I think Mitch is right, you probably need to change your second case to WHEN compliance >= 100.0 (note the >= )
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.