Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encountered arithmetic overflow error using decimal datatype in SQL server 2008

I have a column declared as decimal(4,4). Currently it stores 0.0400 as the value. Now I need to update the value stored from 0.0400 to 9.95.I used the following query :

Update <tablename>
set <columnname>= 9.95
where <condition>

When I try to execute, I get the following error : Arithmetic overflow error converting numeric to data type numeric. The statement has been terminated.

Kindly help.

like image 300
Mekha Avatar asked Jan 23 '26 07:01

Mekha


2 Answers

Defining a column as decimal(4,4) is the equivalent of saying

I will have 4 digits in total, of which 4 come after the decimal point

To represent 9.95, you'd need to store it as 9.9500 to satisfy the '4 decimal places' condition. But this now exceeds the 'max 4 digits' condition and so can't be converted.

You'd need at least decimal(5, 4) to store 9.95 as a decimal in this way.

like image 200
Widor Avatar answered Jan 25 '26 00:01

Widor


If you write decimal(4,4), what the database hears is:

  • There are four digits total
  • All four of them are behind the decimal separator

So a decimal(4,4) can store the range 0.0000 to 0.9999 (and its negative equivalent). But 9.95 is outside that range, so it will return an error.

like image 35
Andomar Avatar answered Jan 24 '26 22:01

Andomar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!