Remove all spaces from a string in SQL Server

后端 未结 23 773
死守一世寂寞
死守一世寂寞 2020-11-28 01:42

What is the best way to remove all spaces from a string in SQL Server 2008?

LTRIM(RTRIM(\' a b \')) would remove all spaces at the right and left of th

相关标签:
23条回答
  • 2020-11-28 02:10

    100% working

    UPDATE table_name SET  "column_name"=replace("column_name", ' ', ''); //Remove white space
    
    UPDATE table_name SET  "column_name"=replace("column_name", '\n', ''); //Remove newline
    
    UPDATE table_name SET  "column_name"=replace("column_name", '\t', ''); //Remove all tab
    

    You can use "column_name" or column_name

    Thanks

    Subroto

    0 讨论(0)
  • 2020-11-28 02:10

    If there are multiple white spaces in a string, then replace may not work correctly. For that, the following function should be used.

    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
    

    Example:

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

    Output:

    aaaaaaaaaa
    
    0 讨论(0)
  • 2020-11-28 02:10

    Syntax for replacing a specific characters:

    REPLACE ( string_expression , string_pattern , string_replacement )  
    

    For example in the string "HelloReplaceThingsGoing" Replace word is replaced by How

    SELECT REPLACE('HelloReplaceThingsGoing','Replace','How');
    GO
    
    0 讨论(0)
  • 2020-11-28 02:11

    If it is an update on a table all you have to do is run this update multiple times until it is affecting 0 rows.

    update tableName
    set colName = REPLACE(LTRIM(RTRIM(colName)), '  ', ' ')
    where colName like '%  %'
    
    0 讨论(0)
  • if you want to remove spaces,-, and another text from string then use following :

    suppose you have a mobile number in your Table like '718-378-4957' or ' 7183784957' and you want replace and get the mobile number then use following Text.

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

    Result :-- 7183784957

    0 讨论(0)
  • 2020-11-28 02:15

    This does the trick of removing the spaces on the strings:

    UPDATE
        tablename
    SET
        columnname = replace(columnname, ' ', '');
    
    0 讨论(0)
提交回复
热议问题