You could also look into Fulltext indexing.
Main Topics
Browse All TopicsHope someone can help me with this. In my [select statement - where clause] I would like to do search on multiple words that user types in. In short give me results where my column matches all the words or any of the words user is trying to do search for.
Table A
----------------------
Column1| Column2
----------------------
1 | steam trap
2 | steam
3 | trap
4 | heat trap
5 | steam engine
6 | xyz
Now for instance, User types 'steam trap' in search box. User should see records from 1 to 5
I can do something like :
Select * from Table A where Column2 like '%steam%trap%'.
but this wont get me record 2,3,4,5.
I can also do something like:
Select * from Table A where Column2 in('steam', 'trap')
But this won't get me record 1,4,5.
I can get around with this by using split function in .net and then doing looping. Then dynamically build where clause. This will work but I am looking for something that can be done right within select statement. I am not looking for any SQL loop scripts. This has to be part of plain select statement.
Short & Sweet:
I'm looking for something as good as this-->
Select * from Table A where Column2 in('%steam%', '%trap%')
And above syntax won't work as is not acceptable in SQL.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
sm394 wrote:
declare @SearchIn nvarchar(1000)
set @SearchIn ='steam, trap'
select * from yourTable
where charindex(rtrim(Column2),@
--------------------------
This won't work. As it will look for exact match i.e 'steam, trap' as one word.
ragbn wrote:
Select * from Table A where Column2 like '%steam%' OR Column2 like '%trap%'
This should work for you
--------------------------
This will work but as I said I will have to dynamically create where clause. Since I won't know what
end-users will try to do search on. It could be 'A B' or 'A B C' or 'A B C D'.
And for right now this seems to be the only solution so far.
I wish there was something like:
Select *
from tableA
where column1
contains any words like(%'steam%trap%abc%efg%
i.e steam, trap, abc or efg.
ScottPletcher wrote:
And if you want to put those with more matches at the top:
SELECT *
FROM [Table A]
WHERE Column2 LIKE '%steam%'
OR Column2 LIKE '%trap%'
ORDER BY
CASE WHEN Column2 LIKE '%steam%' THEN 1 ELSE 0 END +
CASE WHEN Column2 LIKE '%trap%' THEN 1 ELSE 0 END DESC
--------------------------
Thanx. Your this solution did help me to solve my other issue with order by. This will come handy.
ScottPletcher wrote:
You could also look into Fulltext indexing.
--------------------------
THIS DID THE TRICK!!! ---> HATS OFF to ScottPletcher :)
After defining full-text index on tableA - Column1, I can now do search for any number of words in column2. If search includes any special characters it still works. For instance if my search text is
'Steam, Trap'. Full text indexing will consider comma(',') as another word and won't treat 'Steam comma(',') as one word. Sleek!
I HAVE THE POWER OF SQL..
HE-MAN
select * from Table A
WHERE FREETEXT (Column2, 'Steam Trap' )
And if records in my table were:
Table A
----------------------
Column1| Column2
----------------------
1 | steam trap
2 | steam
3 | trap
4 | heat trap
5 | steam engine
6 | xyz
Resultset will bring back records 1,2,3,4,5.
All points goes to ScottPletcher. Thank you.
Business Accounts
Answer for Membership
by: ragbnPosted on 2009-02-03 at 13:01:31ID: 23542202
Select * from Table A where Column2 like '%steam%' OR Column2 like '%trap%'
This should work for you