SQL server integer vs decimal SUM

Cemre Mengü picture Cemre Mengü · Nov 16, 2011 · Viewed 14k times · Source

This is probably a newb question but...I was working on a db in SQSH and it had integer type values that were large (a population attribute). When I wanted to sum these up I got an arithmetic overflow exception. Then I cast the values to Decimal and everything worked OK (no overflow). However, everywhere I read says that Integer and Decimal are the same thing. So why is this happening ?

Thanks in advance

Answer

Sparky picture Sparky · Nov 16, 2011

An integer type is a 4 byte number that can go as high as 2,147,483,647. A decimal can go substantially higher. Both are stored as integers, but the decimal allows for a value to represent digits past the decimal sign. You could also use BIGINT (9,223,372,036,854,775,807) in place of integer.