删除字符串sql server中的所有空格

发布于 2025-01-09 17:03:00 字数 134 浏览 2 评论 0原文

我正在尝试将这个“232431 K”转换为这个“232431K”,但我找不到执行此操作的函数。我找到的所有参考文献都是 TRIM、RTRIM 和 LTRIM,但在这种情况下这些函数对我来说没有用,因为我没有要删除的左侧或右侧空格,空格位于字符串“内部”。

I'm trying to convert this '232431 K' into this '232431K' and I can't find a function that does this. All the references I have found are TRIM, RTRIM and LTRIM but these functions are not useful for me in this case because I don't have left or right white spaces to be removed, the white spaces are "inside" the string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

青衫儰鉨ミ守葔 2025-01-16 17:03:00

您可以参考在SQL Server中删除字符串中的所有空格< /a>.

简单来说,您需要使用 REPLACE 函数,例如 REPLACE(stringValue, ' ', '')

You can refer to Remove all spaces from a string in SQL Server.

In simple terms, you will need to use REPLACE function, such as REPLACE(stringValue, ' ', '')

酒解孤独 2025-01-16 17:03:00

除了使用 replace 之外,在 SQL Server 2017+ 上为了避免多个嵌套函数,如果要删除字符串中有多个字符,还可以使用 translate

删除空格、连字符、斜线:

declare @string varchar(50)='12345 67-9\x'

select Replace(Translate(@string, ' -\','???'),'?','')

In addition to using replace, on SQL Server 2017+ to avoid multiple nested functions, you can use translate if there are multiple characters in a string you want to remove:

Remove space, hyphen, slash:

declare @string varchar(50)='12345 67-9\x'

select Replace(Translate(@string, ' -\','???'),'?','')
无需解释 2025-01-16 17:03:00

也许尝试 替换如下

SELECT  REPLACE(N'232431 K',' ','')

正如另一条评论中所问的

如果我必须替换“ ”或“-”,是否可以仅使用一个 REPLACE 函数将其全部替换?例如 REPLACE(stringValue, [' ', '-'], '')

您应该将替换与 Translate 函数结合使用

select replace(translate (N'l;[pi-',';[-','   '),' ','')

Maybe try REPLACE like below

SELECT  REPLACE(N'232431 K',' ','')

As asked in another comment

What if I have to replace ' ' or '-', is it possible to put it all using just one REPLACE function? For example REPLACE(stringValue, [' ', '-'], '')

you should use replace in conjuction with Translate function

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