Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer Max value constants in SQL Server T-SQL?

Are there any constants in T-SQL like there are in some other languages that provide the max and min values ranges of data types such as int?

I have a code table where each row has an upper and lower range column, and I need an entry that represents a range where the upper range is the maximum value an int can hold(sort of like a hackish infinity). I would prefer not to hard code it and instead use something like SET UpperRange = int.Max

like image 801
AaronLS Avatar asked Apr 29 '10 20:04

AaronLS


People also ask

What is the max value for INT in SQL Server?

The range of an int data type is -2,147,483,648 to 2,147,483,647.

How do I limit integers in SQL?

If you want to limit the range of an integer column you can use a check constraint: create table some_table ( phone_number integer not null check (phone_number between 0 and 9999999999) );

What is the maximum value an integer can hold?

The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing. It is therefore the maximum value for variables declared as integers (e.g., as int ) in many programming languages.


Video Answer


2 Answers

There are two options:

  • user-defined scalar function
  • properties table

In Oracle, you can do it within Packages - the closest SQL Server has is Assemblies...

like image 143
OMG Ponies Avatar answered Sep 22 '22 11:09

OMG Ponies


I don't think there are any defined constants but you could define them yourself by storing the values in a table or by using a scalar valued function.

Table

Setup a table that has three columns: TypeName, Max and Min. That way you only have to populate them once.

Scalar Valued Function

Alternatively you could use scalar valued functions GetMaxInt() for example (see this StackOverflow answer for a real example.

You can find all the max/min values here: http://msdn.microsoft.com/en-us/library/ms187752.aspx

like image 37
vfilby Avatar answered Sep 20 '22 11:09

vfilby