marcAntoine
marcAntoine

Reputation: 678

INT type with Max Size in SQL Server database

How can we indicate a max size for the type int, integer or number in a SQL Server database?

For example, with varchar we can indicate the max size: varchar(40).
What's the equivalent for the numbers?

Upvotes: 2

Views: 8673

Answers (2)

marc_s
marc_s

Reputation: 754993

You define a CHECK constraint:

CREATE TABLE dbo.YourTable
(
   SomeIntColumn INT
      CONSTRAINT CHK_YourTable_SomeIntColumn 
        CHECK (SomeIntColumn >= 0 AND SomeIntColumn <= 9999),
   .......
)

You can define a range of possible values for your numeric column that way. With this CHECK constraint in my sample, you can limit your INT column to a maximum of 4 digits.

Update

For the INT style datatypes, you cannot define a number of digits in SQL Server directly - there's no INT(4) or anything like that. You'll need to use a CHECK constraint as I showed.

The types decimal (and numeric) do support precision and scale:

decimal(p, s)

where p is the precision (total number of digits) and s is the scale (number of digits after the decimal point).

So a decimal(10,4) gives you 10 digits in total, 4 of which are after the decimal point, so you can have values like

     0.0000
123456.1234

and so on

Upvotes: 5

whastupduck
whastupduck

Reputation: 1166

They are already defined by that type you use, for bigint that would be 8 bytes. See reference

Upvotes: 1

Related Questions