Skip to content
Advertisement

SQL Server : select all rows where Column does not contain any value from dynamic table of values

I’m using SQL Server 2016. I am searching TableA and want it to not return any rows where one of the terms from TableB exists in a particular column of TableA.

Assume I have the following sample tables:

In table @SearchTerms, I have ABC and DEF. I want to select * from table @MyStrings where string value does not contain ABC or DEF.

Something like this:

Advertisement

Answer

If the search terms aren’t nullable, you can left join the search terms using LIKE and filter for all rows, where the search term is null.

db<>fiddle

If they are nullable excluding them in the ON clause might work.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement