Remove Trailing Spaces and Update in Columns in SQL Server

前端 未结 13 1050
抹茶落季
抹茶落季 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 20:50

    To remove Enter:

    Update [table_name] set
    [column_name]=Replace(REPLACE([column_name],CHAR(13),''),CHAR(10),'')
    

    To remove Tab:

    Update [table_name] set
    [column_name]=REPLACE([column_name],CHAR(9),'')
    
    0 讨论(0)
  • 2020-12-22 20:53
    update MyTable set CompanyName = rtrim(CompanyName)
    
    0 讨论(0)
  • 2020-12-22 20:53

    SQL Server does not support for Trim() function.

    But you can use LTRIM() to remove leading spaces and RTRIM() to remove trailing spaces.

    can use it as LTRIM(RTRIM(ColumnName)) to remove both.

    update tablename
    set ColumnName= LTRIM(RTRIM(ColumnName))
    
    0 讨论(0)
  • 2020-12-22 20:54

    I had the same problem after extracting data from excel file using ETL and finaly i found solution there :

    https://www.codeproject.com/Tips/330787/LTRIM-RTRIM-doesn-t-always-work

    hope it helps ;)

    0 讨论(0)
  • 2020-12-22 20:55

    To just trim trailing spaces you should use

    UPDATE
        TableName
    SET
        ColumnName = RTRIM(ColumnName)
    

    However, if you want to trim all leading and trailing spaces then use this

    UPDATE
        TableName
    SET
        ColumnName = LTRIM(RTRIM(ColumnName))
    
    0 讨论(0)
  • 2020-12-22 20:56

    Use the TRIM SQL function.

    If you are using SQL Server try :

    SELECT LTRIM(RTRIM(YourColumn)) FROM YourTable
    
    0 讨论(0)
提交回复
热议问题