简体   繁体   中英

Insert zero before a numeric datatype in SQL Server

I was trying insert a value in a column in SQL Server which is of type numeric(18, 0) .

I am unable to insert a zero at the beginning. For example adding 022223 gets inserted as 22223 ...

I think changing the column type to varchar will work but I don't want to alter table structure.

Any way to do this without changing the table structure..Please help

There is no point to have this IN a database. You will, afterall, select the data, won't you? So while selecting do something like this . I looked in google for "mssql numeeric leading zeros". All of the solutions are like in the link I have mentioned :) Or obviously, use varchar if you, for some reason, must have data like that in a table :)

you can apply a transformation when reading and inserting the value like this:

when inserting value :

string s = "00056";
 double val = double.Parse("0." + s);

and when querying value use:

double value = 0.00056; // stored value in your db field
 s = val.ToString().Remove(0,2);

i think it will work in any case - whether you have leading zero in your value or not.

Well I dont think it's possible, since its being stored as a 32 bit so simply add leading zeros when printing it ... There are multiple approaches. http://sqlusa.com/bestpractices2005/padleadingzeros/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM