1
votes

Im using full text search to find rows that have a word near another word, like:

select *
from mytable where
contains (mycolumn, 'someword1 near someword2')

From now on, my input will be a table, like:

declare @words table  
( 
    word varchar(100) not null 
) 

insert into @words (word) values ('someword1') 
insert into @words (word) values ('someword2') 
insert into @words (word) values ('someword3') 

Is it possible to rewrite the query to get the same result, or I ll need to transform the @words rows in a single line of text?

1
All three words in the @words table should be near each other? - Tomalak
yeah, everything in @words should be near each other. - user989818

1 Answers

1
votes

How about this:

declare @words table (word varchar(100) not null) 
insert into @words (word) values ('someword1') 
insert into @words (word) values ('someword2') 
insert into @words (word) values ('someword3') 

declare @q varchar(4000)
set @q = ''
select @q = @q + ' ~ ' + word from @words  -- join all words
set @q = substring(@q, 4, 4000)            -- remove the first ' ~ '

select *
  from mytable
 where contains (mycolumn, @q)