3

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.

0
3

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:

2
  • 1
    TINYINT isn't signed Feb 15 '19 at 17:31
  • Neither is bit :D
    – Dai
    Feb 19 at 23:14
0

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.

2
  • Doing 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.
    – Dai
    Feb 19 at 23:18
  • It's up to the OP how to handle that. Some algorithms depend on silent overflow. Feb 19 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.