Avatar of dkilby
Flag for Canada asked on

MS SQL Query + multiple like '%%'

i want to create a stored procedure that pulls a list from a table.

currently I am searching a comments field for keywords, using a like statement for each keyword, but I would like to pull the list of keywords from another table so if the keywords change i dont have to change the query.  Is this possible?

example of what i am currently doing.

select * from tblCustomerInfo
where comments like '%Test%' or comments like '%One%' or comments like '%Two%'
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
Scott Pletcher

Sure.  Load "Test", "One", etc. into a table and then  you can do this:

FROM tblCustomerInfo ci
    EXISTS(SELECT 1 FROM otherTable o WHERE ci.comments LIKE o.string)

it doesnt work, i put the keywords in a separate table and get nothing back, i test by doing this, and returned rows.

FROM tblCustomerInfo ci
    EXISTS(SELECT 1 FROM KeyWords o WHERE ci.comments LIKE '%One%')
Harish Varghese

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thank you - thank worked
Your help has saved me hundreds of hours of internet surfing.
Scott Pletcher

You need to put the %s in the strings in the table, NOT in the SQL SELECT.

Sorry, I should have stated:

Sure.  Load "%Test%", "%One%", etc. into a table and then do the EXISTS checks.

It's more flexible that way -- there may be times when you don't want leading and/or trailing %s.