在SQL中删除所有空格并将多行合并为一行
在SQL Server 2014中,删除字符串中所有空格的最佳方法是什么 我的字符串是:在SQL中删除所有空格并将多行合并为一行,sql,sql-server,sql-server-2014,Sql,Sql Server,Sql Server 2014,在SQL Server 2014中,删除字符串中所有空格的最佳方法是什么 我的字符串是: Maximize your productivity for building engaging, beautiful web mapping applications 正在尝试删除字符串之间的enter和tab空格以及单词之间的1个空格。结果应该是: Maximize your productivity for building engaging, beautiful web mapping app
Maximize your productivity for building engaging,
beautiful web mapping applications
正在尝试删除字符串之间的enter和tab空格以及单词之间的1个空格。结果应该是:
Maximize your productivity for building engaging, beautiful web mapping applications
您可以使用replace,但这有点棘手:
select replace(replace(replace(replace(replace(col, ' ', '<>'
), '
', '<>'
), ' ', '<>' -- tab goes here
), '><', ''
), '<>', ' '
)
from t;
其思想是将空格替换为,然后将所有>如果对UDF打开,则以下内容将删除所有控制字符和重复空格 删除重复空格的灵感来自几个月前戈登的回答 范例 返回
Maximize your productivity for building engaging, beautiful web mapping applications
UDF如果感兴趣
它适用于例如X Enter y string,但对于X Enter y Enter Z不起作用这是一个可重用的用户定义函数的很好候选。请参阅更新的答案。我删除了32个。没有必要删除值32。。。不需要/多余。
Declare @S varchar(max) = 'Maximize your productivity for building engaging,
beautiful web mapping applications'
Select [dbo].[svf-Str-Strip-Control](@S)
Maximize your productivity for building engaging, beautiful web mapping applications
CREATE FUNCTION [dbo].[svf-Str-Strip-Control](@S varchar(max))
Returns varchar(max)
Begin
Select @S=Replace(@S,char(n),' ')
From (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30),(31) ) N(n)
Return LTrim(RTrim(Replace(Replace(Replace(@S,' ','><'),'<>',''),'><',' ')))
End
--Select [dbo].[svf-Str-Strip-Control]('Michael '+char(13)+char(10)+'LastName') --Returns: Michael LastName