Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Trailing spaces are ignored in SQL Server 2008

, 23 Jun 2011
Rate this:
Please Sign up or sign in to vote.
When comparing data stored in databases, you don't need to use RTRIM()

Create a table:

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

Insert some values for the test:

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:

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)

Share

About the Author

zvin

Canada Canada
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 23 Jun 2011
Article Copyright 2011 by zvin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid