Search code examples
sql-servernestedwildcardsql-likesql-in

SQL Server: Comparing a list of strings with wildcards using LIKE


I'm trying to find a way to compare a character field with a few string wildcards stored in a lookup table. For example, Table A is my main table. There is a column called "Code" in Table A which I need to evaluate. I want to find all rows WHERE Code LIKE "ABC%", "A%", or "A12%". These three strings are stored in a lookup table called Table B. I am trying to avoid hard-coding these wildcards at all costs. Is there a way to do something like WHERE A.Code LIKE (SELECT * FROM B) ?

Thank you!


Solution

  • You do it like below using INNER JOIN:

    SELECT TableA.* FROM TableA 
    INNER JOIN TableB 
    ON TableA.Code LIKE TableB.Code + '%'