Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i have a search panel in my website. when user hit search button i will check thier search keywords are available in my title or content.. when user give a single word the search is working fine but when user give more then one words it will give results but not am expected.. I want to split the keyword string by space and check every words in title or content? How to write the StoredProcedures for my request?

note: if user search football means result is gud.. but when user give two words like "Sony Music player" means like keyword match the exact words with title or content.. So it will give very few results.. i want to split the "Sony Music player" keywords as Sony, Music, player and check every one the keywords with title or content?
Posted

Example made just for fun:
SQL
--temporary table
DECLARE @tmp TABLE(aField NVARCHAR(500))
--insert sample data
INSERT INTO @tmp (aField)
VALUES('Sony Bla bla bla'), ('Son Of The Music'), ('Paranoya Player')
--input text
DECLARE @inputText NVARCHAR(300) = 'Sony Music Player'
--split input text into words ;)
;WITH InputParts AS
(
    SELECT LEFT(@inputText, CHARINDEX(' ', @inputText) -1) AS Word, RIGHT(@inputText, LEN(@inputText) - CHARINDEX(' ', @inputText)) AS Remainder
    WHERE CHARINDEX(' ', @inputText)>0
    UNION ALL
    SELECT LEFT(Remainder, CHARINDEX(' ', Remainder) -1) AS Word, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(' ', Remainder)) AS Remainder
    FROM InputParts
    WHERE CHARINDEX(' ', Remainder)>0
    UNION ALL
    SELECT Remainder AS Word, NULL AS Remainder
    FROM InputParts
    WHERE CHARINDEX(' ', Remainder)=0
)
SELECT t1.aField, @inputText AS SearchedText, t2.Word AS MatchedWord  
FROM @tmp AS t1 INNER JOIN InputParts AS t2 ON LOWER(t1.aField) LIKE CONCAT('%', LOWER(t2.Word),'%')


Result:
aField	            SearchedText        MatchedWord
Sony Bla bla bla	Sony Music Player	Sony
Son Of The Music	Sony Music Player	Music
Paranoya Player	    Sony Music Player	Player
 
Share this answer
 
v2
Comments
SnvMohan 15-Dec-14 2:30am    
i just want a answer for select from database method not from temp table... and when i try to use this query the follwing error has been occured ''CONCAT' is not a recognized built-in function name'..
Maciej Los 15-Dec-14 2:43am    
First of all, you did not provide information about SQL server version. To replace CONCAT, use simple concatenation via using string1 + string2 + string3
I'm using temporary table, because i don't want to create real table in my database. You can use real table in your query.
Basically, what you need to do is convert your string into the separate words "sony", "music", and "player", and then issue a query that assembles them together:
SQL
...WHERE MyColumn LIKE '%sony%' OR MyColumn LIKE '%music%' OR MyColumn LIKE '%player%'
Or
SQL
...WHERE MyColumn LIKE '%sony%' AND MyColumn LIKE '%music%' AND MyColumn LIKE '%player%'

There is no automatic way to do that, but it's not complex - just rather tedious!
There is code here: Converting comma separated data in a column to rows for selection[^] that separates a commad delimited string "sony,music,player" to separate words - it would be trivial to change that to use spaces instead. Then, instead of creating a temporary table, you could assemble an SQL query as a string and EXEC it to produce your results list.
 
Share this answer
 
first of all i will get split function and used query like this...

Select * from ProductsTB inner join dbo.Split(@keyword, ' ') as tokens on (Description like '%'+ tokens.Items +'%' or Title like '%' + tokens.Items + '%')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900