1

I have a column in my SQL Server database and it has white spaces from left and right site of the record. Basically it's a nvarchar(250) column.

I have tried removing white spaces completely like this:

UPDATE MyTable 
SET whitespacecolumn = LTRIM(RTRIM(whitespacecolumn)) 

But this didn't work out at all, the whitespace is still there. What am I doing wrong here?

7
  • 1
    What characters are the white space? Trim only removes spaces. Not tabs or other types of white space. Commented May 21, 2017 at 18:15
  • Im not sure which kind of charachters they are, but theres an empty space in front of the records like this: (this space here) username Commented May 21, 2017 at 18:17
  • @MartinSmith How to remove the tabs/enter ? Commented May 21, 2017 at 18:18
  • You need to know by what number they are represented in particular encoding. For example in UTF-8, 13 number is for space, then you can replace it with REPLACE(col, char(13), ''). Since I am not sure about encoding (most surely it's UTF-8), I would suggest trying yourself. Commented May 21, 2017 at 18:22
  • @MichałTurczyn this goes with update statement ? Commented May 21, 2017 at 18:22

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

1

I think replace is the way as you are looking to update

UPDATE MyTable SET whitespacecolumn = Replace(whitespacecolumn, ' ', '')

you can try doing select first and then prefer to update

SELECT *, Replace(whitespacecolumn, ' ', '') from MyTable

LTRIM, RTRIM will remove spaces in front and rear of column. In 2016 you can use TRIM function as below to trim special characters as well:

SELECT TRIM( '.,! ' FROM  '#     test    .') AS Result;

Output:

# test

1 Comment

Support for the TRIM() function is available in SQL Server 2017; as far as I can tell, it is not available in SQL Server 2016 or below. learn.microsoft.com/en-us/sql/t-sql/functions/trim-transact-sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.