Why does SQL Server not pull expected results when using strings with comparison operators?

The question is based on an observation I had while working on something else. It is not stopping any production or development research work, but is more about satisfying my thirst for knowing why such a behaviour is being seen.

We can use comparison operators with strings as well. Hence, I tried to use the following query on a SQL Server 2012 instance with the sample AdventureWorks2012 database (the collation of the database and of the column is the default: SQL_Latin1_General_CP1_CI_AS):

USE AdventureWorks2012 ;
GO

--Returns 5 records
SELECT  pp.Name
FROM    Production.Product AS pp
WHERE   pp.Name >= N'Short' AND pp.Name <= N'Sport' ;
GO

Open in new window


The query only returns 5 records. This despite the fact that the search is an inclusive search and the Production.Product table contains records that begin with "Sport".

String Search - Query 01 results - only 5 records
Now, when I replace "Sport" with "Sporu" (just moving one character up in the alphabet to verify whether characters after the word have any impact on the search) gives me 8 records.

USE AdventureWorks2012 ;
GO

--Returns 8 records
SELECT  pp.Name
FROM    Production.Product AS pp
WHERE   pp.Name >= N'Short' AND pp.Name <= N'Sporu' ;
GO

Open in new window


String Search - Query 02 results - Observe 8 records are fetched
What's going on inside of SQL Server that allows it to fetch "Short-Sleeve Classic Jersey" for the starting word "Short" but prevents it from fetching "Sport-100 Helmet" for the ending word "Sport" despite the search being an inclusive search?
LVL 15
Nakul VachhrajaniTechnical Architect, Capgemini IndiaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SimonCommented:
>This despite the fact that the search is an inclusive search and the Production.Product table contains records that begin with "Sport".

When using the comparison  <= 'Sport', the string 'Sport' is the last possible match, so other strings starting with 'Sport' will be outside the matched range e.g. 'Sport-a', 'Sporta', 'sport ' are all > 'Sport'.

If you had the exact values 'Short' and  'Sport' in your table they would  both be included in the query results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nakul VachhrajaniTechnical Architect, Capgemini IndiaAuthor Commented:
I see your point now. "Sport-100 Helmet" is actually > "Sport" and hence fails the check for <= "Sport".

Thank-you for the quick response!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.