Click here to Skip to main content
15,867,594 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

Trailing spaces are ignored in SQL Server 2008

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Jun 2011CPOL 16.2K   2   2
When comparing data stored in databases, you don't need to use RTRIM()

Create a table:


SQL
CREATE TABLE dbo.TestTrailingSpaces
(
   id		int identity(1,1) primary key,
   SomeName	varchar(20)
)

Insert some values for the test:


SQL
insert into dbo.TestTrailingSpaces (SomeName)
values('aaa ')
insert into dbo.TestTrailingSpaces (SomeName)
values('bbb     ')

Now run these Select statements and all of them will return you values:


SQL
select * from dbo.TestTrailingSpaces where SomeName = 'aaa'
select * from dbo.TestTrailingSpaces where SomeName = 'aaa     '
select * from dbo.TestTrailingSpaces where SomeName = 'bbb'

Summary: Trimming on both sides is redundant, because trailing blanks are ignored by "=".
Consider this, trimming the column prevents an index seek, this could be vital for performance.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 new knowledge Pin
Don Jomar Hombrebueno27-Jun-11 20:10
Don Jomar Hombrebueno27-Jun-11 20:10 
GeneralReason for my vote of 5 Nice tip, I didn't know this. Pin
Keith.Badeau27-Jun-11 15:48
Keith.Badeau27-Jun-11 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.