Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I had UserTable, in the subject column i had text like 'SQL Server 2005'. User is searching the subject from webapplication. When user search with 'SQL Server 2005' he will get the result. Here requirement is he need to get the result even he search in other orders like 'Server SQL 2005' or 'SQL 2005 Server' or '2005 Server SQL'. I tried all the possibilities using like operator but no success. Please help me.
r

Regards,
Rahul
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-13 21:09pm    
Not informative. Where is your statement? How do you want anyone to help?
—SA

There is no a simple operator in SQL Server that will do the match. You need to use a user function for this.

First, I suggest reading this wonderful question[^] on stack overflow. It will teach you how to write a user function that returns a table. Then, you can grab any of the proposed solution for the Split function and write your own function called MatchPhrase.

Suppose, you've taken this solution for your split function (it is not my function, I just pasted it from the link above):

SQL
create function dbo.SplitString 
    (
        @str varchar(4000), 
        @separator char(1)
    )
    returns table
    AS
    return (
        with tokens(p, a, b) AS (
            select 
                1, 
                1, 
                charindex(@separator, @str)
            union all
            select
                p + 1, 
                b + 1, 
                charindex(@separator, @str, b + 1)
            from tokens
            where b > 0
        )
        select
            p-1 zeroBasedOccurance,
            substring(
                @str, 
                a, 
                case when b > 0 then b-a ELSE 4000 end) 
            AS s
        from tokens
      )
    GO


Now, all you need is to write this simple function:

SQL
CREATE FUNCTION dbo.MatchPhrase
(
    @Phrase1 varchar(500),
    @Phrase2 varchar(500)
)
RETURNS INT
AS
BEGIN
    DECLARE @Result INT
    SET @Result = 0
    IF NOT EXISTS((SELECT s FROM dbo.SplitString(@Phrase1, ' ') 
                   EXCEPT 
                   SELECT s FROM dbo.SplitString(@Phrase2, ' '))
                  UNION ALL
                  (SELECT s FROM dbo.SplitString(@Phrase2, ' ') 
                   EXCEPT 
                   SELECT s FROM dbo.SplitString(@Phrase1, ' '))
                  )
        SET @Result = 1
    RETURN @Result
END
GO


And that's it. All you need to do is write this SQL statement:

SQL
SELECT * FROM UserTable WHERE dbo.MatchPhrase(subject, 'SQL Server 2005') = 1


Use this SQL Fiddle[^] as a playground
 
Share this answer
 
You would have to split the input string into separate words...

SQL
Server
2008

then

select *
from table
where column like '%word1%'
and column like '%word2%'
and column like '%word3%'
 
Share this answer
 
Comments
chaau 21-Jan-13 22:42pm    
the above solution will also match:
'SQL Server 2008 is the best'
'I hate SQL Server 2008'
_Maxxx_ 21-Jan-13 22:49pm    
Yes - I assumed you'd want to match those and not only match exactly the words entered - without knowing the complete context, this may or not be what is required

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