Here is a really simple solutions to create and unsigned int on ms sql server. The following has been tested on sql server 2008. Unfortunately there is no native support for an unsigned int but we can force the functionality with using a big int and creating a set of rules based on a custom type. If we really still wanted to create an unsigned int we could of course use a .net clr class and build our own type. But that seems like overkill.

CREATE TYPE dbo.uint
      FROM bigint
GO

CREATE RULE uint_range
      AS @i >= 0 AND @i <= 4294967295
GO

EXEC sp_bindrule 'uint_range', 'uint'
GO

These sorts of rules are also useful for creating any custom type. eg phone number or such as well. The major difference is the rule is on the type not on the table constraint so they can be changed in all table's where they are being used at the same time.





Last Modified: 21 February 2017

Releated Posts


2012-08-07 - MSSQL - Finding the database restore history
2011-09-24 - MSSQL - Checking Uptime
2011-08-11 - MSSQL - Saving memory by using stored procedures
2011-08-09 - MSSQL - Last Backup time and size
2011-06-24 - MSSQL - Extracting part of an email address
2011-06-15 - MSSQL - Bulk Deleting rows with backup
2011-06-07 - MSSQL - DBCC Check All Databases
2011-06-03 - MSSQL - Who locks what
2011-05-26 - MSSQL - RandomString Function
2011-05-22 - MSSQL - Removing the aspnet membership database
2011-05-19 - MSSQL - ShrinkLog
2011-05-16 - MSSQL - Enum
2011-05-05 - MSSQL - Log Sizes
2011-03-01 - MSSQL - Kill connections by host
2011-02-23 - C Sharp / MSSQL Get inserted value of NEWSEQUENTIALID()
2011-02-22 - MSSQL - Kill connections by username
2011-02-20 - MSSQL - All temp tables are global
2011-02-20 - MSSQL - Unsigned int
2011-02-17 - MSSQL - Convert IP To big int
2011-02-05 - MSSQL - Levenshtein
2011-02-02 - MSSQL - Kill connections to a database
2011-02-01 - MSSQL - Convert unix timestamp to date time
2011-01-30 - MSSQL - Enabling CLR Functions
2011-01-30 - MSSQL - RegEx Support
2011-01-30 - MSSQL - Adding MD5 checksum support
2011-01-30 - MSSQL - Adding SHASum support
2011-01-29 - MSSQL - TRIM Function