admin

删除所有空格并在SQL中将多行合并为单行

sql

在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 applications

阅读 266

收藏
2021-06-07

共1个答案

admin

如果对UDF开放,则以下内容将删除所有控制字符和重复的空格。

几个月前, 戈登的* 答案启发了重复空间的移除( 确定被盗 )。 *

例子

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

UDF(如果有兴趣)

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
2021-06-07