SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Search Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

duro
Starting Member

5 Posts

Posted - 11/18/2012 :  11:38:12  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/18/2012 :  12:31:32  Show Profile  Reply with Quote
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 - 11/18/2012 :  16:09:59  Show Profile  Reply with Quote
no, sorry i want :
select * from table where name like '% car %' or name like '% red %'
any ideas? thanks!
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/18/2012 :  17:27:53  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000