views:

127

answers:

1

I'm trying to write an insert statement for a SQL Server table that inserts the value 1 into a decimal field. The field is of the type decimal(10, 10) which, as far as I understand, means that it can have up to 10 digits altogether, and up to 10 of those digits can be after the decimal point. But, when I try to run the insert statement I get the following error:

Arithmetic overflow error converting int to data type numeric.

If I change the data type of the field to decimal(11, 10), it suddenly works. What am I not understanding here? What am I doing wrong?

+10  A: 

decimal(10, 10) means all decimal places, no digits to the left of the decimal point!

see here: http://msdn.microsoft.com/en-us/library/aa258832(SQL.80).aspx_

decimal[(p[, s])]

p (precision) Specifies the maximum total number of decimal digits that can 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. The maximum precision is 38. The default precision is 18.

s (scale) Specifies the maximum number of decimal digits that can be stored to the right of the decimal point. Scale must be a value from 0 through p. Scale can be specified only if precision is specified. The default scale is 0; therefore, 0 <= s <= p. Maximum storage sizes vary, based on the precision.

decimal(11,10) gives you 1 digit the the left of the decimal and 10 to the right, so integer 1 fits now!

EDIT

when using: decimal(p,s), think of p as how many total digits (regardless of left or right of the decimal point) you want to store, and s as how many of those p digits should be to the right of the decimal point.

DECIMAL(10,5)=     12345.12345
DECIMAL(10,2)=  12345678.12
DECIMAL(10,10)=         .1234567891
DECIMAL(11,10)=        1.1234567891
KM
I'm still confused. It says that scale is the " *maximum* number of decimal digits that can be stored to the right of the decimal point" - not that there has to be 10.
froadie
Yes, but it doesn't mean it's a sliding decimal point. The implication is if you provide less than 10 digits to the right, the rest are zeroes.
Joe
@Joe, I'm not sure exactly what you are trying to say, but if you run `declare @x decimal(5,2);set @x=1.123456789;select @x;set @x=1;select @x` you will get `1.12` and `1.00`. Once you declare a decimal, the number of digits to the left and to the right of the decimal point are fixed, there is no sliding of the decimal point.
KM
@froadie, if you want 10 whole number digits and 10 decimal digits in a value: `1234567891.1234567891`, use `DECIMAL(20,10)`
KM
@KM, I was responding to froadie's comment, not your post which I +1'd.
Joe