SQL Server decimal scale length - can be or has to be? -


i have question decimal (and maybe numeric) type in sql server 2008 r2.

msdn said:

(scale) maximum number of decimal digits can stored right of decimal point. scale must value 0 through p.

i understand following way:

  • if have decimal(10, 5) - able store 12345.12345 or 12345678.91.
  • if have decimal(5, 5) - can have 12345 or 1234.5 or 1.2345, etc...

is clear?

but got error message:

select cast(2.8514 decimal(5,5)) 

arithmetic overflow error converting numeric data type numeric.

i thought 5,5 means can have 5 digits , 5 can right of decimal point.

as tried:

select cast(12.851 decimal(6,5)) - overflows 

however

select cast(1.23456 decimal(6,5)) - ok. 

so what's truth?

decimal(a,b) says can have digits , b of them right decimal point (and there rest a-b left dec. point)?

i'm confused statement in doc copied everywhere. please take while , explain me simple thing.

lot of thanks!

the easiest way think of (for me) precision total number of digits, of which scale number of digits right of decimal point. decimal(p,s) means p-s digits left of point, , s digits right of point.

that explains conversion errors you're seeing: 2.8514 cannot decimal(5,5) because p-s = 0; 12.851 cannot decimal(6,5) because p-s = 1 , on.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -