Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL equivalent of C# BigInteger

So, I've been using the new BigInteger class introduced in .NET 4 to store incredibly large numbers.

Now, I've started thinking about saving these numbers to the database (SQL Server). My question is, is this even possible? As far as I know and what I've researched, the SQL data types do not contain a property that corresponds to the behaviour of BigInteger (theoretically no min or max).

The closest I've seen is bigint, but that has a minimum of -2^63 (-9,223,372,036,854,775,808) and a maximum of 2^63-1 (9,223,372,036,854,775,807).

like image 464
mattytommo Avatar asked Dec 02 '25 05:12

mattytommo


2 Answers

If your SQL server's DECIMAL type does not support the precision you need you'll only have the option to store the number as a string or a binary, i.e. in a VARCHAR, TEXT or VARBINARY field.

like image 152
ThiefMaster Avatar answered Dec 04 '25 19:12

ThiefMaster


You could store the result of calling yourBigInt.ToByteArray() in a VARBINARY(…), subsequently retrieving it using the BigInteger(byte[]) constructor.

The main disadvantage of this approach is that SQL Server would be restricted in its ability to work with the values as numbers (probably limited to comparing them). Then again, that's a foregone conclusion for anything that won't fit into a bigint.

like image 34
Marcelo Cantos Avatar answered Dec 04 '25 20:12

Marcelo Cantos