Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with my CharIndex

Author  Topic 

jodders
Starting Member

41 Posts

Posted - 2014-09-24 : 05:45:30
Morning all,
I am putting a query together to bring back an exact Name match through a list of names in my database. For example, If I was looking for people called "Parker" in my list, I have certain variations such as
Mr.John Parker
Parker John Spider
John Spider Parker
Parkert

I want my query to bring back names with the Word "Parker". So using the example above, it should only bring back the Top 3.

I have experimented with the function "Like" but that brings results that I don't want. I also thought using the CharIndex function but that doesn't work either unless I am doing something wrong?




CHARINDEX('Parker',name) > 0


Can you help?

Many thanks
J

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-09-24 : 09:06:59
One way perhaps would be to allow for a leading or trailing space like this:
NAME LIKE '% Parker %' OR NAME LIKE 'Parker %' OR NAME LIKE '% Parker'
Go to Top of Page

mmkrishna1919
Yak Posting Veteran

95 Posts

Posted - 2014-09-24 : 09:22:06
You can also use PATINDEX as below

WHERE PATINDEX('%parker%',name) > 0

M.MURALI kRISHNA
Go to Top of Page

jodders
Starting Member

41 Posts

Posted - 2014-09-24 : 09:47:39
Hi, thanks so much for your guidance on this!

I have tried both and I think i will have to use James K method just because the Patindex is bringing back "Parkers" as well. I didn't know Patindex existed so that is something else i have learnt today.

Cheers for you help :)
Go to Top of Page
   

- Advertisement -