插入不带尾随空格的字符串 SQL

发布于 2024-09-01 02:22:02 字数 290 浏览 5 评论 0原文

我有一个数据库,其中包含一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街角卖回忆 2024-09-08 02:22:02

如果您需要可变长度字符串,请使用 nvarchar(100) 而不是 nchar(100)。后者始终有 100 个字符,前者最多可以有 100 个字符,但不会填满空间。

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.

你是我的挚爱i 2024-09-08 02:22:02

插入时使用sql LTRIMRTRIM 函数。

Use the sql LTRIM and RTRIM functions when inserting.

爱情眠于流年 2024-09-08 02:22:02

您是否可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文