Storing an int to SQL, but keep leading zeros

12,087

Numeric datatypes do not retain leading zeros, as they are insignificant to the number you want to store. Char or Varchar is more appropriate. You could set a constraint to ensure only numeric characters are stored.

If you absolutely cannot change the data type, then another alternative is to store the number of leading zeros into another int field

So in your example you would store:
Value : 32
Leading zeros : 2

Share:
12,087
OJazem
Author by

OJazem

Updated on June 26, 2022

Comments

  • OJazem
    OJazem almost 2 years

    I have a field in my SQL Server 2012 table defined as Int. but when I try to get the value from a textbox in C# using the converting (Convert.toint32(textbox.text)). Lets say if the textbox contains the number 0032, it will be saved to the database as 32 removing the 00.

    Any solutions other than changing the Field Data Type??