1

I have a database with a field named Field1 that has 100 nchars per entry. Each time I add a value, it is stored as:

"value     (100-ValueLength Spaces)        "

So Basically each stored value has a string of spaces after it. This is getting to be an issue when I try doing:

if (value == "Example")

because of all of the empty spaces after the string. How can I get it so the stored values don't have all of these trailing spaces?

1
  • Not sure, although I think it is plain SQL I'm using Microsoft SQL Server Management Studio 2008 if that helps at all. Commented May 13, 2010 at 17:36

3 Answers 3

9

If you want a variable-length string, use nvarchar(100) instead of nchar(100). The later always has 100 characters, the former can have up to 100 characters, but doesn't fill up the space.

Sign up to request clarification or add additional context in comments.

2 Comments

Got it. When I started building my database, I didn't know the difference between nchar and nvarchar, and opted to use nchar, and now I realize the mistake I made.
Useful answer. When might someone want to use nchar over nvarchar?
3

Use the sql LTRIM and RTRIM functions when inserting.

2 Comments

I'm using LINQ to insert, is there a LINQ equivalent to what you're talking about?
Then simply apply Trim to the string, there's no need to use sql functions in this case.
2

Are you able to use a nvarchar, so that way there isnt padding added if you don't meet the required string length. If so that might be better then constantly having to trim your string entry.

1 Comment

I just read up on the difference between nvarchar and nchar. I will try implementing this field type and see if that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.