删除字符串 sql 服务器中的所有空格
Remove ALL white spaces in a string sql server
我正在尝试将此“232431 K”转换为此“232431K”,但找不到执行此操作的函数。我找到的所有参考文献都是 TRIM、RTRIM 和 LTRIM 但这些函数在这种情况下对我没有用,因为我没有左或右空格删除后,空格位于字符串“内部”。
也许试试 REPLACE 像下面这样
SELECT REPLACE(N'232431 K',' ','')
正如另一条评论中所问
What if I have to replace ' ' or '-', is it possible to put it all using just one REPLACE function? For example REPLACE(stringValue, [' ', '-'], '')
您应该将替换与翻译功能结合使用
select replace(translate (N'l;[pi-',';[-',' '),' ','')
可以参考Remove all spaces from a string in SQL Server.
简单来说就是需要用到REPLACE
函数,比如REPLACE(stringValue, ' ', '')
除了使用 replace
,在 SQL Server 2017+ 上避免多重嵌套函数,如果要删除的字符串中有多个字符,您可以使用 translate
:
删除 space、连字符、斜杠:
declare @string varchar(50)='12345 67-9\x'
select Replace(Translate(@string, ' -\','???'),'?','')
我正在尝试将此“232431 K”转换为此“232431K”,但找不到执行此操作的函数。我找到的所有参考文献都是 TRIM、RTRIM 和 LTRIM 但这些函数在这种情况下对我没有用,因为我没有左或右空格删除后,空格位于字符串“内部”。
也许试试 REPLACE 像下面这样
SELECT REPLACE(N'232431 K',' ','')
正如另一条评论中所问
What if I have to replace ' ' or '-', is it possible to put it all using just one REPLACE function? For example REPLACE(stringValue, [' ', '-'], '')
您应该将替换与翻译功能结合使用
select replace(translate (N'l;[pi-',';[-',' '),' ','')
可以参考Remove all spaces from a string in SQL Server.
简单来说就是需要用到REPLACE
函数,比如REPLACE(stringValue, ' ', '')
除了使用 replace
,在 SQL Server 2017+ 上避免多重嵌套函数,如果要删除的字符串中有多个字符,您可以使用 translate
:
删除 space、连字符、斜杠:
declare @string varchar(50)='12345 67-9\x'
select Replace(Translate(@string, ' -\','???'),'?','')