views:

254

answers:

1

I want to get all the record from the one table which contain atleast one word from the input string. Ex: input parameter='Stack over flow':

select * 
  from sample 
 where name like '%stack%' 
    or name like '%over%'
    or name like '%flow%'

I want to search record which contains 'stack' or ' over' or 'flow' ...

+5  A: 

The code you gave should work. But don't use it. It won't be able to use any indexes, and so will likely be very slow. Look into a full text index and the CONTAINS keyword instead.

Joel Coehoorn
Short of that, a regex could be used if CLR is enabled.
OMG Ponies
It sounds like there is only one input parameter that he needs parsed and then a like for each word.
Kelsey
Absolutely correct. Like with a wildcard as the first character is almost always a bad thing as it forces a table scan and full-text indexing will work much better.
HLGEM
I agree using leading wildcards with the LIKE is going to make this perform really badly, but if the table is small it won't matter that much. If it is a large table definitely full text index with contains. It appears he already has an answer from a different question on how to split the parameter into words.
GrowlingDog
@OMG - a Regular expression would still need to perform a table scan
Kragen