views:

84

answers:

1

Hi folks,

I'm trying to create the following sql query (as in, this is an example of the final query) :-

DECLARE @SearchQuery AS NVARCHAR(100) = 'yellow bird'

SELECT Id, Name
FROM dbo.FooBars
WHERE CONTAINS(Name, N'FORMSOF(Thesaurus, yellow)') 
  AND CONTAINS(Name, N'FORMSOF(Thesaurus, bird)')

Notice how i've got two 'CONTAINS' lines? This is because the search query has two words in it. (a space is the delimeter). This query could be from 1 to n words.

How can i generate this sql code based upon the number of words in the search query?

+3  A: 

You can put the "AND" in the contains itself, so it coud be

select * 
from dbo.FooBars
where contains(Name, 'FORMSOF(Thesaurus, yellow) AND FORMSOF(Thesaurus, bird)')

with the string 'FORMSOF(Thesaurus, yellow) AND FORMSOF(Thesaurus, bird)' built up into a variable like

declare @searchCriteria varchar(200)
set @searchCriteria = 'Some string you built up'
select * 
from dbo.FooBars
where contains(Name, @searchCriteria)
Tetraneutron
Winnah. Wikid. cheers mate :)
Pure.Krome