Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Search Query

Author  Topic 

duro
Starting Member

5 Posts

Posted - 2012-11-18 : 11:38:12
hello,

right now on my search page the sql query i am using is similar to:

select * from table where name like '%search%'

and it returns the exact phrases perfectly

however if search = "car red" there are no results

i want it to be able to return "red card" results also

basically i want it to be:

select * from table where name like '%car%' or name like '%red%'

how do i break up the value of search for each term or phrase

any ideas? thanks!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-18 : 12:31:32
When you search for "car red", do you want to return rows that contain words like "scary", "Fred", "redo" etc.?
Go to Top of Page

duro
Starting Member

5 Posts

Posted - 2012-11-18 : 16:09:59
no, sorry i want :
select * from table where name like '% car %' or name like '% red %'
any ideas? thanks!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-18 : 17:27:53
Copy the code in Fig. 21 in this article to install the function: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Then use it like this:
DECLARE @search VARCHAR(255) = 'car apple';

SELECT
t.*
FROM
YourTable t
INNER JOIN dbo.DelimitedSplit8K(@search,' ') d
ON ' ' + t.name + ' ' LIKE '% ' + d.Item + ' %'
Go to Top of Page
   

- Advertisement -