Sum (INT) error: Arithmetic overflow error when converting expression to data type int

Source: Internet
Author: User

In SQL Server, the data of a column is within the int range, but when sum is used to calculate the sum of the column, an Arithmetic overflow error occurs when expression is converted to an int type ".

First, let's take a look at the length of the data type defined by SQL Server:

    • Bigint data type Stores numbers in the range of-2 ^ 63 (-9223372036854775808) to 2 ^ 63-1 (9223372036854775807. The storage size is 8 bytes.
    • The storage range of the int data type is-2,147,483,648 to 2,147,483,647 (each value requires 4 bytes of storage space ).
    • The storage range of smallint data types is only-32,768 to 32,767 (each value requires 2 bytes of storage space ), the tinyint data type can only store numbers ranging from 0 to 255 (each value requires a storage space of 1 byte ).

 

When we perform the sum operation on Int type data, Arithmetic overflow may occur. In this case, we can change the data type of the original column to bigint before performing the sum operation.

 

SQL statement:

1. Select sum (cast (colname as bigint) from tablename

2. Select sum (convert (Numeric (20, 0), colname) from tablename

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.