Search code examples
sqlsql-servert-sqluser-defined-typesuser-defined

object doesn't contain any of suffixes defined in user-defined table SQL


guys!

I don't know how to select objects that do not contain suffixes. I declare input table of suffixes

  @suffixes  dbo.tvp_stringArray READONLY

And then I'm selecting my objects

SELECT [Object]
FROM [myUsers]
WHERE [Object] IS NOT LIKE (SELECT suffix FROM suffixes)

How to add '%' to the selected suffix?


Solution

  • As others have pointed out, you can concatenate the % to the suffix when using the LIKE operator.

    But to take into account the fact that you only want the objects that does not match any of the suffixes, assuming there are more than one, you could add NOT EXISTS like this:

    SELECT *
    FROM MyUsers 
    WHERE NOT EXISTS (SELECT 1 FROM Suffixes WHERE MyUsers.Object LIKE '%' + suffix );
    

    SQL Fiddle