sooprise
sooprise

Reputation: 23187

Inserting Strings Without Trailing Spaces SQL

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?

Upvotes: 1

Views: 3645

Answers (3)

Justin Gregoire
Justin Gregoire

Reputation: 2583

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.

Upvotes: 2

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41306

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.

Upvotes: 9

thelost
thelost

Reputation: 6694

Use the sql LTRIM and RTRIM functions when inserting.

Upvotes: 3

Related Questions