6

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.

5
  • What is the datatype of thecolumn?
    – Quassnoi
    May 3, 2011 at 13:45
  • Where exactly are you seeing the trailing spaces (on a web page, in SSMS)? May 3, 2011 at 13:46
  • The data type is varchar May 3, 2011 at 13:46
  • @Joe from SSMS, I copy paste the value from the grid to the editor May 3, 2011 at 13:48
  • 1
    Are you sure they are actually spaces, not tabs or something. If your column is varchar the first query should have worked (though the second query won't work as trailing spaces are ignored in such comparisons) May 3, 2011 at 13:49

6 Answers 6

14

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

2
  • OMG it's a char(9)!! Thanks for the tip! May 3, 2011 at 13:54
  • Тhat's strange. This solve my issue too, so +1, but what's the real difference between "HARD" and "SOFT" spaces?
    – gotqn
    Aug 30, 2013 at 13:33
8

Are you certain that it is a space (ascii 32) character? You can get odd behavior with other "non-visible" characters. Try running

select ascII(right(theColumn, 1))
 from theTable

and see what you get.

2
  • 1
    Thanks! You are right, it's char(9), not char(32). But Crassy answered earlier, so I give the answer to him. Upvoted! May 3, 2011 at 13:56
  • 2
    FYI, char(9) is the Tab character. May 3, 2011 at 13:57
3

Use this Function:

Create Function [dbo].[FullTrim] (@strText varchar(max)) Returns varchar(max) as Begin

Declare @Ch1 char,@ch2 char
Declare @i int,@LenStr int
Declare @Result varchar(max)
Set @i=1
Set @LenStr=len(@StrText)
Set @Result=''


While @i<=@LenStr
Begin
    Set @ch1=SUBSTRING(@StrText,@i,1)
    Set @ch2=SUBSTRING(@StrText,@i+1,1)
    if ((@ch1=' ' and @ch2=' ') or (len(@Result)=0 and @ch1=' '))

        Set @i+=1

    Else
        Begin
            Set @Result+=@Ch1
            Set @i+=1

        End

End

Return @Result End

1

In SQL, CHAR(n) columns are right-padded with spaces to their length.

Also string comparison operators (and most functions too) do not take the trailing spaces into account.

DECLARE @t TABLE (c CHAR(10), vc VARCHAR(10))

INSERT
INTO    @t
VALUES  ('a   ', 'a    ')

SELECT  LEN(c), LEN(vc), с + vc
FROM    @t

--
1    1    "a         a"

Please run this query:

SELECT  *
FROM    thetable
WHERE   thecolumn + '|' <> RTRIM(thecolumn) + '|'

and see if it finds something.

1
  • In SQL Server VARCHAR are not right trimmed try SELECT '[' + vc + ']' FROM @t. Think ANSI_PADDING might affect this though. (At least that's what it says here msdn.microsoft.com/en-us/library/ms187403.aspx but doesn't seem to make any difference this end!) May 3, 2011 at 13:51
0

It sounds like either:

1) Whatever you are using to view the values is inserting the trailing space (or the appearance thereof- try a fixed-width font like Consolas).

2) The column is CHAR, not VARCHAR. In that case, the column will be padded with spaces up to the length of the column, e.g. inserting 'abc' into char(4) will always result in 'abc '

3) You are somehow not committing the updates, not updating the right column, or other form of user error. The update statement itself looks correct on the face of it.

0

I had the same issues with RTRIM() AND LTRIM() functions.

In my situation the problem was in LF and CR chars.

Solution

DECLARE @test NVARCHAR(100)
SET @test = 'Declaration status  '

SET @test = REPLACE(REPLACE(@test, CHAR(10), ''), CHAR(13), '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.