Remove all spaces from a string in SQL Server

后端 未结 23 914
死守一世寂寞
死守一世寂寞 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 01:58

    Check and Try the below script (Unit Tested)-

    --Declaring
    DECLARE @Tbl TABLE(col_1 VARCHAR(100));
    
    --Test Samples
    INSERT INTO @Tbl (col_1)
    VALUES
    ('  EY     y            
    Salem')
    , ('  EY     P    ort       Chennai   ')
    , ('  EY     Old           Park   ')
    , ('  EY   ')
    , ('  EY   ')
    ,(''),(null),('d                           
        f');
    
    SELECT col_1 AS INPUT,
        LTRIM(RTRIM(
        REPLACE(
        REPLACE(
        REPLACE(
        REPLACE(
        REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(
            REPLACE(col_1,CHAR(10),' ')
            ,CHAR(11),' ')
            ,CHAR(12),' ')
            ,CHAR(13),' ')
            ,CHAR(14),' ')
            ,CHAR(160),' ')
            ,CHAR(13)+CHAR(10),' ')
        ,CHAR(9),' ')
        ,' ',CHAR(17)+CHAR(18))
        ,CHAR(18)+CHAR(17),'')
        ,CHAR(17)+CHAR(18),' ')
        )) AS [OUTPUT]
    FROM @Tbl;
    

提交回复
热议问题