简体   繁体   English

插入不带空格的字符串SQL

[英]Inserting Strings Without Trailing Spaces SQL

I have a database with a field named Field1 that has 100 nchars per entry. 我有一个数据库,名为Field1的字段每个条目具有100个nchar。 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? 我如何获得它,以便存储的值没有所有这些尾随空格?

If you want a variable-length string, use nvarchar(100) instead of nchar(100) . 如果要使用可变长度的字符串,请使用nvarchar(100)而不是nchar(100) The later always has 100 characters, the former can have up to 100 characters, but doesn't fill up the space. 后者总是有100个字符,前者最多可以有100个字符,但不占空间。

插入时使用sql LTRIMRTRIM函数。

Are you able to use a nvarchar, so that way there isnt padding added if you don't meet the required string length. 您是否可以使用nvarchar,这样,如果您不符合所需的字符串长度,就不会添加填充。 If so that might be better then constantly having to trim your string entry. 如果这样的话可能会更好,那么就必须不断调整字符串条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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