Remove Trailing Spaces and Update in Columns in SQ

2019-03-07 18:47发布

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 those, and I want to have the data without any trailing spaces.

The company name is like "Amit Tech Corp "

I want the company name to be "Amit Tech Corp"

13条回答
手持菜刀,她持情操
2楼-- · 2019-03-07 19:38

Try SELECT LTRIM(RTRIM('Amit Tech Corp '))

LTRIM - removes any leading spaces from left side of string

RTRIM - removes any spaces from right

Ex:

update table set CompanyName = LTRIM(RTRIM(CompanyName))
查看更多
Root(大扎)
3楼-- · 2019-03-07 19:41

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))
查看更多
老娘就宠你
4楼-- · 2019-03-07 19:42

The best way to remove all the spaces is SELECT REPLACE("Amit Tech Corp ",' ','')

Instead of LTRIM & RTRIM

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-03-07 19:44

Example:

SELECT TRIM('   Sample   ');

Result: 'Sample'

UPDATE TableName SET ColumnName = TRIM(ColumnName)
查看更多
闹够了就滚
6楼-- · 2019-03-07 19:45

Use the TRIM SQL function.

If you are using SQL Server try :

SELECT LTRIM(RTRIM(YourColumn)) FROM YourTable
查看更多
看我几分像从前
7楼-- · 2019-03-07 19:49
update MyTable set CompanyName = rtrim(CompanyName)
查看更多
登录 后发表回答