Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Nothing Just curiosity

Author  Topic 

ZarrinPour
Yak Posting Veteran

66 Posts

Posted - 2008-11-01 : 14:23:01
Hi all
Could anyone tell me why the following code causes exception?

declare @a decimal(4,4)
Set @a=2.95
print @a

Msg 8115, Level 16, State 8, Line 3
Arithmetic overflow error converting numeric to data type numeric.

But According to the BOL:

decimal[ (p[ , s] )]

p (precision)
The maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point
s (scale)
The maximum number of decimal digits that can be stored to the right of the decimal point. and 0<=s<=p

Thanks in advance.
Kind Regards.


Nothing is quite impossible!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-11-01 : 15:23:51
Apparently, s can not equal p. You'll get that exception if you 3,3 4,4 5,5 ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-11-01 : 15:30:57
decimal(4, 4) indicates that you want 4 digits total and that you want 4 of those digits to the right of the decimal place, hence 0 to the left. 2.95 has 1 digit to the left of the decimal place, therefore it can't fit.

The following works fine, as there's no digit left of the decimal place.
declare @a decimal(4,4)
Set @a=0.295
print @a

--
Gail Shaw
SQL Server MVP
Go to Top of Page

ZarrinPour
Yak Posting Veteran

66 Posts

Posted - 2008-11-02 : 11:35:32
Thank you GilaMonster

Nothing is quite impossible!
Go to Top of Page
   

- Advertisement -