Does Microsoft SQL Server support unsigned integer data types?
I am trying to learn a bit more about the data type choices and helping a friend in his database project, I know that MySQL does.
No. The numeric data types are all signed.
For example an int is from approx. -2^31 to +2^31.
If you have a value just shy of 2^32 you will have to use a larger data type such as bigint. There are finer points here, but out of scope of this discussion.
Related Q & A:
Why aren't unsigned integer types available in the top database platforms?
Documentation:
You can easily emulate unsigned 32-bit int with your regular, signed bigint. Do whatever arithmetics needs to be done, AND with 0xFFFFFFFF for every operation that may potentially overflow the lower 32 bits.
AND 0xFFFFFFFF
would just sweep-under-the-rug any overflow issues - I feel a CHECK CONSTRAINT
to set a bounds on the range would be better as that would throw an error on overflow rather than hiding it. Another problem with using a wider type (e.g. bigint
instead of a hypothetical utin32
) is that you end-up wasting 4 bytes per column per record, which could add-up. In a recent application I built I ended-up storing uint64
values as signed bigint
values and transforming values (and query parameters) at the last minute - which added more complexity.