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

Trailing spaces are ignored in SQL Server 2008

By , 23 Jun 2011
 

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)

About the Author

zvin
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 new knowledgememberDon Jomar Hombrebueno27 Jun '11 - 20:10 
GeneralReason for my vote of 5 Nice tip, I didn't know this.memberKeith.Badeau27 Jun '11 - 15:48 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 23 Jun 2011
Article Copyright 2011 by zvin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid