Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server 4 byte unsigned int

Does anyone know of any work around by which i can save unsigned integers (0 to 4294967295) simply using 4 bytes instead of using 8 bytes and bigint?

I know we can create user defined datatypes and create a constraint on them to not allow negative values but that still does not allow me to enter values over 2147483647. I only want to use 4 bytes but be able to save integer values greater than 2147483647 but less than 4294967295.

Possible duplicate: 4 byte unsigned int in SQL Server?

like image 578
Raj Avatar asked Oct 02 '09 14:10

Raj


People also ask

Does SQL Server support unsigned INT?

Some DBMSs, like MariaDB and MySQL, allow to create UNSIGNED columns. SQL Server doesn't support unsigned integers.

What is unsigned data type in SQL?

The “unsigned” in MySQL is a data type. Whenever we write an unsigned to any column that means you cannot insert negative numbers. Suppose, for a very large number you can use unsigned type. The maximum range with unsigned int is 4294967295. Note: If you insert negative value you will get a MySQL error.

How many digits can INT hold in SQL Server?

The INTEGER data type stores whole numbers that range from -2,147,483,647 to 2,147,483,647 for 9 or 10 digits of precision.

Can INT be unsigned?

In C, unsigned is also one data type in which is a variable type of int this data type can hold zero and positive numbers. There is also a signed int data type in which it is a variable type of int data type that can hold negative, zero, and positive numbers.


2 Answers

Use a Binary(4). Wrap it in a UDT if you want.

like image 43
RBarryYoung Avatar answered Sep 28 '22 05:09

RBarryYoung


There is no unsigned type available to you, so you could create one using the UDT, or opt for the larger data type. If you do it in a UDT you are going to exceed the 4 bytes again.

The extreme hack would be to apply an offset automatically to your stored value after you read it, by adding -2^31 but this is a real hacky way to go about it and confusing for anyone viewing the code etc, not to mention the potential for mistakes / things being missed. I wouldn't recommend the hack at all.

like image 130
Andrew Avatar answered Sep 28 '22 05:09

Andrew