插入不带尾随空格的字符串 SQL
我有一个数据库,其中包含一个名为 Field1 的字段,每个条目有 100 个 nchar。 每次我添加一个值时,它都会存储为:
"value (100-ValueLength Spaces) "
所以基本上每个存储的值后面都有一串空格。当我尝试这样做时,这成为一个问题:
if (value == "Example")
因为字符串后面有所有空格。 我怎样才能得到它,这样存储的值就不会有所有这些尾随空格?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要可变长度字符串,请使用
nvarchar(100)
而不是nchar(100)
。后者始终有 100 个字符,前者最多可以有 100 个字符,但不会填满空间。If you want a variable-length string, use
nvarchar(100)
instead ofnchar(100)
. The later always has 100 characters, the former can have up to 100 characters, but doesn't fill up the space.插入时使用sql
LTRIM
和RTRIM
函数。Use the sql
LTRIM
andRTRIM
functions when inserting.您是否可以使用 nvarchar,这样如果不满足所需的字符串长度,就不会添加填充。如果是这样,那可能会比不断修剪字符串条目更好。
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.