Page 1 of 1

SUM broken

Posted: Fri Sep 13, 2002 1:26 pm
by MiniMe
I have a table that has a column that is smallint. When I do a sum on that column it comes out to 25,770 when it should be closer to 7,361,830. Whats the deal?

SUM broken

Posted: Fri Sep 13, 2002 2:12 pm
by John
It's summing in the type of the column, which will overflow. Try

sum(convert(col, 'long'));

SUM broken

Posted: Fri Sep 13, 2002 2:12 pm
by mark
smallint will only hold 16 bit values so it rolls over when summing. convert to a data type large enough for the sum.
sum(convert(Field,'int'))