Solved

SQL contains/exists matching data, something as LIKE operator

Posted on 2009-03-31
10
782 Views
Last Modified: 2013-12-12
I am trying to figure out a way in SQL to match a comma separated field with another value.
For example.

select * from tbl_test where fld_search like '%Canton, Ohio, Biology , Chemistry%

In the database I have the word "Biology"
Now, the statement above doesn't work at all.
Any way to compare two fields ?

Thanks much.
0
Comment
Question by:martyje
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 24029663
You should use IN operator for this

select * from tbl_test where fld_search IN ('Canton', 'Ohio', 'Biology' , 'Chemistry')

Hope this helps
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24029691
The easiest thing to do would be to load up a temp table, and then join to it:


CREATE TABLE #tmp (SearchTerm varchar(1000))
INSERT INTO #tmp (SearchTerm) VALUES ('Canton')
INSERT INTO #tmp (SearchTerm) VALUES ('Ohio')
INSERT INTO #tmp (SearchTerm) VALUES ('Biology')
INSERT INTO #tmp (SearchTerm) VALUES ('Chemistry')

SELECT *
FROM tbl_test t1 INNER JOIN
      #tmp t2 ON t1.fld_search LIKE '%' + t2.SearchTerm + '%'

DROP TABLE #tmp




That can be put into an sproc, to make it easier to run from PHP...
0
 
LVL 15

Expert Comment

by:MNelson831
ID: 24029692
The % is a wild card so your query is looking for any data string that has the exact phrase

"Canton, Ohio, Biology , Chemistry"

In it.  To search for just the word "biology" you need to say;

like('%biology%')

You can also search for things like this:

Like('%biology%ohio%chemistry')

which will return anything that has all 3 words, in that order.

Another option for searching for partial strings is this:

where charindex('Biology', MyTextField) > 0
0
 
LVL 5

Expert Comment

by:catherinelouise
ID: 24029735
If it is your database field that is comma separated and contains multiple values, you may be looking for something like:

SELECT * FROM tbl_test
WHERE fld_search LIKE '%Canton%'
OR fld_search LIKE '%Ohio%'
OR fld_search LIKE '%Biology%'
OR fld_search LIKE '%Chemistry%'
0
 

Author Comment

by:martyje
ID: 24029885
Thanks much for the quick responds.
IN the statement:
select * from tbl_test where fld_search like '%Canton, Ohio, Biology , Chemistry%

"Canton, Ohio, Biology , Chemistry" data is coming from another table. It's one field. have data separated with commas.



Any thoughts?
Thanks ya all.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 5

Expert Comment

by:catherinelouise
ID: 24029909
I think the OR statement I put above should work - did you try it?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 24030824
You can do this as a "quick and dirty" way to make it work, although it does give poor performance vs other methods.

select *
from tbl_test
where CHARINDEX(',' + RTRIM(fld_search) + ','  ,  ',' + 'Canton, Ohio, Biology, Chemistry' + ',') > 0
0
 

Author Closing Comment

by:martyje
ID: 31564864
I changed , to ', ' in my data and used ur logic above and am back in business. thanks.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24031206
1.) You should "READ" the "COMMA SEPERATED VALUES" in to an array via PHP implode
2.) Loop throught the array and make an SQL with individual LIKE = '%value%' lines
3.) Execute SQL statement
4.) Check data
5.) Output results

I'll Post the code next
=NerdsOfTTech
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24031260
Nevermind your answer was found. Solution canceled.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
The viewer will learn how to count occurrences of each item in an array.

930 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

10 Experts available now in Live!

Get 1:1 Help Now