Remove Trailing Spaces and Update in Columns in SQL Server

前端 未结 13 1143
抹茶落季
抹茶落季 2020-12-22 20:42

I have trailing spaces in a column in a SQL Server table called Company Name.

All data in this column has trailing spaces.

I want to remove all

13条回答
  •  情书的邮戳
    2020-12-22 21:08

    If we also want to handle white spaces and unwanted tabs-

    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;
    

提交回复
热议问题