在SQL Server 2008中删除字符串中的所有空格的最佳方法是什么?

LTRIM(RTRIM(' a b '))将删除字符串右侧和左侧的所有空格,但我还需要删除中间的空格。


当前回答

一个功能版本(udf),删除空格,cr, lf,制表符或可配置。

select Common.ufn_RemoveWhitespace(' 234   asdf   wefwef 3  x   ', default) as S

结果:234 asdfwefwef3x

alter function Common.RemoveWhitespace
(
    @pString nvarchar(max),
    @pWhitespaceCharsOpt nvarchar(max) = null -- default: tab, lf, cr, space 
)  
returns nvarchar(max) as
/*--------------------------------------------------------------------------------------------------
    Purpose:   Compress whitespace

    Example:  select Common.ufn_RemoveWhitespace(' 234   asdf   wefwef 3  x   ', default) as s 
              -- Result: 234asdfwefwef3x

    Modified    By          Description
    ----------  ----------- --------------------------------------------------------------------
    2018.07.24  crokusek    Initial Version 
  --------------------------------------------------------------------------------------------------*/ 
begin    
    declare 
        @maxLen bigint = 1073741823, -- (2^31 - 1) / 2 (https://stackoverflow.com/a/4270085/538763)
        @whitespaceChars nvarchar(30) = coalesce(
            @pWhitespaceCharsOpt, 
            char(9) + char(10) + char(13) + char(32));  -- tab, lf, cr, space

    declare
        @whitespacePattern nvarchar(30) = '%[' + @whitespaceChars + ']%',
        @nonWhitespacePattern nvarchar(30) = '%[^' + @whitespaceChars + ']%',
        @previousString nvarchar(max) = '';

    while (@pString != @previousString)
    begin
        set @previousString = @pString;

        declare
            @whiteIndex int = patindex(@whitespacePattern, @pString);

        if (@whiteIndex > 0)
        begin                   
            declare 
                @whitespaceLength int = nullif(patindex(@nonWhitespacePattern, substring(@pString, @whiteIndex, @maxLen)), 0) - 1;                

            set @pString = 
                substring(@pString, 1, @whiteIndex - 1) + 
                iif(@whiteSpaceLength > 0, substring(@pString, @whiteIndex + @whiteSpaceLength, @maxLen), '');
        end        
    end        
    return @pString;
end
go

其他回答

如果是对表的更新,则必须多次运行此更新,直到影响到0行为止。

update tableName
set colName = REPLACE(LTRIM(RTRIM(colName)), '  ', ' ')
where colName like '%  %'

由于某种原因,replace每次只对一个字符串起作用。 我有一个像这样的字符串“Test MSP”,我想只留下一个空格。

我使用了@Farhan的方法,但做了一些修改:

CREATE FUNCTION ReplaceAll
(
    @OriginalString varchar(8000),
    @StringToRemove varchar(20),
    @StringToPutInPlace varchar(20)
)
RETURNS varchar(8000)
AS
BEGIN
declare @ResultStr varchar(8000)
set @ResultStr = @OriginalString
while charindex(@StringToRemove, @ResultStr) > 0
    set @ResultStr = replace(@ResultStr, @StringToRemove, @StringToPutInPlace)

return @ResultStr
END

然后像这样运行更新

UPDATE tbTest SET Description = dbo.ReplaceAll(Description, '  ', ' ') WHERE ID = 14225

然后我得到了这个结果: 测试MSP

张贴在这里,如果有人需要它,就像我一样。

上运行: Microsoft SQL Server 2016 (SP2)

如果你想从字符串中删除空格,-和另一个文本,那么使用以下命令:

假设您的表格中有一个手机号码,如“718-378-4957”或 ' 7183784957',你想要替换并获得手机号码,然后使用下面的文本。

select replace(replace(replace(replace(MobileNo,'-',''),'(',''),')',''),' ','') from EmployeeContactNumber

结果:——7183784957

如果字符串中有多个空格,则replace可能无法正常工作。为此,应该使用下面的函数。

CREATE FUNCTION RemoveAllSpaces
(
    @InputStr varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
declare @ResultStr varchar(8000)
set @ResultStr = @InputStr
while charindex(' ', @ResultStr) > 0
    set @ResultStr = replace(@InputStr, ' ', '')

return @ResultStr
END

例子:

select dbo.RemoveAllSpaces('aa  aaa       aa aa                 a')

输出:

aaaaaaaaaa

这是去除字符串上的空格的技巧:

UPDATE
    tablename
SET
    columnname = replace(columnname, ' ', '');