Home > Back-end >  TSQL Column Values used in WHERE...LIKE
TSQL Column Values used in WHERE...LIKE

Time:07-07

Okay so I currently have tables called ModTable, Slots, and I want to pass All of the values from the column [Modules] in ModTable to a LIKE. In effect, I need to search my Slots table, specifically the SlotValue column, for entries matching the entries in ModTable, which I have reformatted to be %//[Module]/%. I did it this way because the values in Slots.SlotValue that I want to pull follow the pattern of %//[Module]/%, but there are multiple modules in the column. My code looks like this:


(
    SELECT [ObjectID]
    FROM [Slots]
    WHERE SlotValue LIKE
    (
        SELECT [Module]
        FROM [ModTable]
    )
)

ModTable:

Modules
%//XYZ/%
%//ABC/%
%//LMN/%

Want:

WHERE [Slots].[SlotValue] LIKE(%//XYZ/% OR %//ABC/% OR %//LMN/%)

CodePudding user response:

SELECT [ObjectID]
FROM [Slots]
JOIN [ModTable] ON SlotValue LIKE ModTable.Module

This might give you more than one row with the same value -- so you can use a distinct if you want.

CodePudding user response:

Use a correlated EXISTS:

SELECT [ObjectID]
FROM dbo.[Slots] S
WHERE EXISTS
(
    SELECT 1
    FROM dbo.[ModTable] MT
    WHERE S.SlotValue LIKE MT.Module
);
  • Related