search string help needed…

  • sqlblue

    SSCrazy

    Points: 2324

    Hi, I need help in getting the result back from the search based on the criteria as follow:

    1) if the user type in the text box the words ‘rest again’ without any quotes around them (the single quotes I put around them here just to differentiate them from the rest), the result should come back as rest OR again OR resting is fine. But nothing should come back that stand before the word rest (so, antirest and anti-rest should not come

    back from the result).

    2) if the user type “rest again” in the box (with the double quotes around them as shown) , the result should come back as the whole 2 words ‘rest again’ (again, the single quote i put around them here just to differentiate from

    the rest).

    Any idea would be appreciated.

    the sample is below.

    create table test

    (

    id int not null,

    string varchar(100) NULL

    )

    GO

    insert into test

    select 1, ‘rest’

    union

    select 2, ‘resting experience’

    union

    select 3, ‘rest again’

    union

    select 4, ‘anti-rest’

    union

    select 5, ‘antirest’

    GO

    create procedure proc_test

    (

    @string varchar(100)

    )

    as

    select * from test

    where string LIKE ‘% ‘ + @string + ‘ %’

    OR string LIKE ‘ ‘ + @string + ‘%’

    OR string LIKE @string + ‘%’

    –drop table test

    –drop procedure proc_test

  • Lowell

    SSC Guru

    Points: 323309

    there’s a great article here on SSC on creating a google like full text search[/url], which does everything you are asking and more; I’d suggest going that way first, as it would make your searches much more robust and professional.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • sqlblue

    SSCrazy

    Points: 2324

    Thanks Lowell for the link. It is very helpful. I couldn’t understand completely his code,

    but what helped me was to look into SQL Server 2008 Full Text Indexing option, which has

    already been available in earlier versions, but I totally forgot about it. Thanks again.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply