Solved

LIKE operator not matching exact strings?

Posted on 2006-10-30
16
5,410 Views
Last Modified: 2009-07-29
Has anybody ever heard of the LIKE operator not matching exact strings? For example I would expect "WHERE lastname LIKE 'Smith%'" to return records like "Smith" and "Smithe". But I'm running into an issue where only "Smithe" is returned, not "Smith". The lastname column is an nvarchar.

One workaround I'm using is "WHERE lastname + ' ' LIKE 'Smith%'". It works, but this is causing a huge performance hit (around 20 times slower). I've tried PATINDEX, instead of LIKE, but it's not very fast either.

I'm looking for any suggestions, articles about this problem, or any confirmation of this problem.

Thanks
0
Comment
Question by:eric0213
  • 4
  • 3
  • 2
  • +3
16 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17833956
look for a space after the "Smith "
in your data

select fname+'X'
and if it comes back Smith X instead of SmithX

you know that's the problem

or use righttrim to remove spaces
where righttrim(lastname) like 'Smith%'
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 17833975
all of the below work for me:


select 'Smith'
where  'Smith' like 'Smith%'

select 'Smith '
where  'Smith ' like 'Smith%'

select 'Smithe'
where  'Smithe' like 'Smith%'

select 'Smithe '
where  'Smithe ' like 'Smith%'


is there something else in your query that may be excluding the Smith row(s)?
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17833979
Sorry.. my example for diagnosis should be

select lastname+'X'

and the trim statement should be

where rtrim(lastname) like 'Smith%'


0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17833984
>Has anybody ever heard of the LIKE operator not matching exact strings?
I did not. Looks curious?!
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17834021
what are you actually doing?

"WHERE lastname LIKE 'Smith%'"

seems to have the wrong number of quotes for dynamic sql....

lastname like 'SMITH%'   will return SMITH AND SMITHE


"WHERE lastname LIKE 'Smith%'"

should be

'WHERE lastname like ''SMITH%'''

that's all single quotes 6 in total





0
 
LVL 32

Expert Comment

by:awking00
ID: 17834024
Perhaps it could be the problem is that the spaces are in front. If so, use lefttrim -
select ...
where lefttrim(lastname) like 'Smith%';
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Author Comment

by:eric0213
ID: 17834075
I looked at the records and there are no spaces on either side. I added an RTrim and it didn't help.

This seems like such a basic thing, I'm not sure why the records don't get returned. I was hoping someone had seen this before, either in an old version of MSSQL, or when using indexed columns, or when there was a full moon.

I've spent a good chunk of morning searching for someone else with this issue, but nothing. I've searched here, the newsgroups, MSDN, and the web (not exhaustively of course).
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 17834088
what does this do on your system?


select 'Smith'
where  'Smith' like 'Smith%'

select 'Smith '
where  'Smith ' like 'Smith%'

select 'Smithe'
where  'Smithe' like 'Smith%'

select 'Smithe '
where  'Smithe ' like 'Smith%'
0
 
LVL 4

Author Comment

by:eric0213
ID: 17834153
knightEknight, they all return one record.

awking00, tried, no luck.

Lowfatspread, the double quotes were just there to delimit what I had in my SQL, they weren't really in the statement.

Essentially, I want my LIKE statement to function like below (LIKE is not case-sensitive). The UPPER function below is costing me as much as appending a space.
     WHERE lastname LIKE 'Smith%' OR UPPER(lastname) = 'SMITH'
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 17834182
If each of the above statements returned one record, then the LIKE operator is working normally on your system.  Is there something else in your query that may be filtering out the expected rows?  Can you post your entire query?
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 250 total points
ID: 17834195
I still think there are invisible characters in your data. That has always been the situation when I have had this kind of problem...


select lastname, ascii(substring(lastname,6,1))
from  mytable where substring(lastname,1,5)='Smith'

Also, can you insert the actual value of 'Smith' into your database temporarily and see if the SQL works on newly inserted data?
0
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17834219
In 20 years of SQL writing I have seen many occasions where things just like this occurred... and in every case the problem was either the data or SQL logic on my side.

So do not look for an error in SQL in handling this simple SQL unless you are running a beta version or something (by the way, in your reply post your version just in case .. select @@version and send us the results)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18062723
my suggestion:
points to AaronAbend, as to his remark of using the ascii function to identify what characters are really in the string to identify the "culprit" data.
angelIII, page editor
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now