SQL INT Data Type

·

·

Use the SQL Server INT data type to define columns, variables, and parameters storing whole numbers. 

The INT data type stores a number in the range -2,147,483,648 to 2,147,483,647.  If you need to store a larger integer value, consider using BIGINT

In addition to being used for numbers, INT is also used to define primary and foreign keys.  Use it to create various ID related column types: 

Converting INT Values 

When converting integers to characters, if the integer has more digits than defined int the CHAR type, an asterisk (*) is output.  Try this SQL to see the effect: 

SELECT CAST(123456 AS char(6)) AS FitsInChar , CAST(1234567 AS char(6)) AS TooBig;
SELECT CAST(123456 AS char(6)) AS FitsInChar 
 , CAST(1234567 AS char(6)) AS TooBig; 

When performing arithmetic, SQL Server converts to the DECIMAL type integer constant values when the values are larger than allowed for INT

SELECT 2147483647 / 2 AS IntegerResult , 2147483648 / 2 AS DecimalResult;
SELECT 2147483647 / 2 AS IntegerResult 
 , 2147483648 / 2 AS DecimalResult;

Notice 2147483648, though when even divide by two, and clearing falling within INT’s boundaries, result as a DECIMAL type. 

At first glance this does not make sense, as the result clearly “fits” within an INT, type; however, the constant value is suspect.  It is convert to DECIMAL prior to the operation, thus coercing the result’s type to DECIMAL

Examples using SQL INT 

Use arithmetical operators, such as +,-, * and / to work with INT. 

SELECT Name , ProductNumber , DaysToManufacture , DaysToManufacture / 2 AS HalfDaysToManuFacture , DaysToManufacture * 2 AS DoubleDaysToManuFacture FROM Production.Product WHERE DaysToManufacture > 1;
SELECT Name 
 , ProductNumber 
 , DaysToManufacture 
 , DaysToManufacture / 2 AS HalfDaysToManuFacture 
 , DaysToManufacture * 2 AS DoubleDaysToManuFacture 
FROM Production.Product 
WHERE DaysToManufacture > 1;

Since the operands, that is the column and number, are integers, the result is also an integer.  

Additional Resources 

To learn more about SQL Server data types, check out these useful resources: 

More from the blog


MySQL PostgreSQL SQLite SqlServer