Not sure if this should be in SQL or .NET but as I’m doing this in asp.net I figured here…
I’m trying to insert ‘00’ into a varchar column of my database, when I do this its actually in the database as ‘0’… if I do ‘05’ or ‘15’ or ‘30’ then its fine.
When you look in the table using SQL Server Management Studio does it have “0” or “00”? If “0” then the issue is in your insert. How are you inserting the “00”? If it is “00” then the issue is in your read. How are you reading from the table?
AFAIK, within certain limits, you can put any string into a varchar field.
So please explain your reply in more detail. It would be much more helpful to know exactly why rather than to simply say “shouldn’t”.
My question is whether or not it should even be varchar. If you are dealing exclusively with numeric values wouldn’t it be better to use a numeric field?
I’m more familiar with PHP and MySQL, so things might be different with .NET and SQL, but MySQL has a way to “pad” fields. So maybe it would be most efficient to change the field’s structure. If not then I’m sure you could do some manipulations on the values using .NET Perhaps type casting?
Good points. If you are putting “00” as a string into a varchar it shoudl be “00” in the database. If you are putting an int into the varchar the int will have a value of 0.
Why not just String.Format the value like this:
String.Format("{0:00}", intToInsert);
More specific information may reveal a better solution.