简体   繁体   中英

Removing spaces from column

I found tons of articles stating you can remove spaces like this:

update mytable set Name=TRIM(Name)
UPDATE mytable SET Name = LTRIM(RTRIM(Name))

My columns is nvarchar have I misunderstood something, because my column names still have spaces in start and end of the values like below:

'Der_j_McD '

Check the below;

  1. Find any special characters like char(10), char(13) etc in the field value.
  2. Check the status of ANSI_PADDING ON. Refer this MSDN article.

Try

UPDATE Table SET [Column] = REPLACE(REPLACE(LTRIM(RTRIM([Column])),CHAR(10),''),CHAR(13),'')

Assuming that you want to trim all spaces, tabs, carriage return and line feeds you need to use the TRIM function like so:

UPDATE t SET name = TRIM(CHAR(13) + CHAR(10) + CHAR(9) + ' ' FROM name)

This requires SQL Server 2017 or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM