Friday, March 30, 2012

Saving to SQL

I am currently migrating my application from Access to SQL. It looks like it's working good but one thing I cannot understand is why when I have created a varchar(40) field and after I insert a field that is less then 40 characters long, the rest of the string is filled with the leading spaces. I have also create an nchar(40) that did not solve it ether. For example: the text is "Hello World". The text is 11 characters long but the data saved to the table is: "Hello Wolrd" + 29 spaces.

Am I doing something wrong?


Thanks for your help in advance.

Use nvarchar(40) instead. The nchar(40) is for a fixed length column.

|||

I have used nvarchar(40) and varchar(40) and both save with the trailing spaces. I have changed one of the tables and it seems it took effect but then the other table still does the same thing. I wonder what I did to get it working. I don't remember the steps I took. Is there something else besides changing the datafields?

|||

You can update your varchar(nvarchar) column with TRIM functions to remove trailing spaces from both ends(just in case).

UPDATE yourTableSET colVarChar40=LTRIM(RTRIM(colChar40))

|||

I am actually Inserting the new value not updating. How can I accomplish this?

|||

please show your insert code.

|||

the update is for after you have done all your inserting just update it.... or in your sproc before you insert the value do the trim

No comments:

Post a Comment