Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot remove trailing space in SQL Server 2005

Tags:

sql-server

This is in SQL Server 2005. I have a varchar column and some rows contain trailing space, e.g. abc, def.

I tried removing the trailing space with this command:

update thetable 
set thecolumn = rtrim(thecolumn)

But the trailing space remains. I tried to find them using:

select * 
from thetable 
where thecolumn <> rtrim(thecolumn)

But it returned nothing.

Are there some settings that I am not aware that influences trailing space check?

EDIT:

I know that there is trailing space from SSMS, when I copy paste the value from the grid to the editor, it has trailing space.

like image 341
Endy Tjahjono Avatar asked May 03 '11 13:05

Endy Tjahjono


People also ask

How do I delete a trailing space in SQL?

The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

Why Ltrim is not working?

The most likely culprits are going to be 13 and/or 10 which are carriage return / line feed respectively. There other characters are what is causing the behavior you are seeing. The LTRIM and RTRIM functions are working perfectly fine, it is your data that is at fault here.

How do you get rid of leading and trailing spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

Which function will be used to remove only the trailing spaces from a string?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.


1 Answers

Check if the spaces that are not removed have the ASCII code 32. Try this to replace "hard space" with "soft space":

update thetable set thecolumn = rtrim(replace(thecolumn, char(160), char(32)))

the query was missing equal sign

like image 68
rickythefox Avatar answered Sep 24 '22 14:09

rickythefox