views:

383

answers:

3

I am using SQL server MSDE 2000. I have a field called notes of type nvarchar(65).

The content is 'Something ' with an extra space after the content (quotes for clarity) in all the records. I used the following command.

UPDATE TABLE1 
   SET notes = RTRIM(LTRIM(notes))

But it does not work. Is there any alternate way to do it?

+1  A: 

Are you sure it's a space and not some other unicode character that just looks like a space?

DanM
+1  A: 

RTRIM should work for this. Are you sure the character afterwards is actually a space?

Is there a problem with the collation you're using, and the display of the character?

womp
+3  A: 

Are you sure the query isn't working? Try:

SELECT TOP 100 '~'+ t.notes +'~'
  FROM TABLE1 t

TOP 100 will limit the results to the first 100 rows, enough to get an idea if there's really a space in the output. If there is, and RTRIM/LTRIM is not removing it - then you aren't dealing with a whitespace character. In that case, try:

UPDATE TABLE1
  SET notes = REPLACE(notes, 
                      SUBSTRING(notes, PATINDEX('%[^a-zA-Z0-9 '''''']%', notes), 1), 
                      '')
WHERE PATINDEX('%[^a-zA-Z0-9 '''''']%', notes) <> 0
OMG Ponies
This one works. What is the difference between this and the one I have used?
bdhar
@Bharanidharan: It removes non printable ASCII characters, which LTRIM/RTRIM does not.
OMG Ponies
Fine. What is a non-printable ACSII character? Is it different from a blank space?
bdhar
The first 32 values are non-printing control characters, such as Carriage Return (decimal value 13) and Line Feed (decimal value 10): http://www.csgnetwork.com/asciiset.html
OMG Ponies
Got it. Thanks :)
bdhar