SQL server integer vs decimal SUM

13,824

Solution 1

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.

Solution 2

From Books OnLine

decimal and numeric

decimal[ (p[ , s] )] and numeric[ (p[ , s] )] Fixed precision and scale numbers. When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1.

int, bigint, smallint, and tinyint

Has a length of 4 bytes, and stores numbers from -2,147,483,648 through 2,147,483,647.

So, Decimal can store much greater value than Int.

Share:
13,824
Cemre Mengü
Author by

Cemre Mengü

I try and catch

Updated on June 05, 2022

Comments

  • Cemre Mengü
    Cemre Mengü almost 2 years

    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