0

I am using ASP.NET MVC and SQL Server and I want to store a 12 digit value which is 221133556677.

This is where I wanted to store the value in, So Int36 can only store up to 10 digit.

So how can I change the data type into numeric(12,0) in order to store the 12 digit value.

[Display(Name = "IC")]
[Required(AllowEmptyStrings = false, ErrorMessage = "IC is required")]
public int IC { get; set; }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

2

So Int36 can only store up to 10 digit.

In all computers in the world htat follow standard architecture, there IS NO SUCH THING AS INT36. Bytes are 8 bits, so it is 32. Not 36.

And since ages, Int64 is a thing too. Which has MUCH MUCH larger scale.

In SQL Server it is named BIGINT and has a scale that may surprise you:

2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)

Case closed?

Oh, no....

So how can I change the data type into numeric(12,0) in order to store the 12 digit value.

Just Do It? Let's start with your C# side code using int - not long. Int is 32 bit (not 26). Just change it to - oh, you insist on using numeric (decimal)? Ah, use Decimal not int. Done. Otherwise I would go with a long and bigint on the database.

Note, though, that this "number" is likely NOT A NUMBER. It is a numeric string. Storing it as number makes little sense if you may need one day to do partial searches and never will use stuff like average, sum etc.

Now, you may want to read some documentation:

https://learn.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql?view=sql-server-ver15

has all SQL Server data types. This helps you not to ignore the obvious larger scale data type.

TomTom
  • 61,059
  • 10
  • 88
  • 148
2

According to the SQL Server documentation you can use BIGINT.

Its a signed 64 bit int and has a range of -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807).

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-data-type-mappings

From what I have been able to find specifying the size of the numeric doesn't effect the size of the number it can store in SQL Server and only affects when ZEROFILL is used

What is the size of column of int(11) in mysql in bytes?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

A long can store 12 digits just fine. So use long instead of int in your C#.

        long twelveDigits = 221133556677;
        Console.WriteLine($"\nHere is twelve digit number, {twelveDigits}.");
        Console.Write("\nPress any key to exit...");
        Console.ReadKey(true);

See here: Long data type MSDocs

And SQLServer has the Data Type bigint See here: int, bigint, smallint, and tinyint (Transact-SQL) and: www.sqlservertutorial.net

These should get you taken care of.