I am looking for something like this but can\'t figure out the best way to write the query:
SELECT DISTINCT CategoryID FROM tbl_Categories c INNER JOIN
ma
DECLARE @Delimitedtext varchar(max);
DECLARE @Delimiter char(1);
SET @Delimitedtext = '234,245,645,';
SET @Delimiter = ',';
;WITH Strings(s, r)
AS
(
SELECT
SUBSTRING(@Delimitedtext,1, CHARINDEX(@Delimiter, @Delimitedtext)-1) s,
SUBSTRING(@Delimitedtext,CHARINDEX(@Delimiter, @Delimitedtext)+1, len(@Delimitedtext)) r
UNION ALL
SELECT
SUBSTRING(r,1, CHARINDEX(@Delimiter, r)-1) s,
SUBSTRING(r,CHARINDEX(@Delimiter, r)+1, len(r)) r
FROM Strings
WHERE
CHARINDEX(@Delimiter, r) > 0
)
SELECT CategoryId
FROM (
SELECT c.CategoryID
FROM tbl_Categories c
JOIN mappingTable mp
ON c.CategoryID = mp.CategoryID
JOIN SubCategories sc
ON mp.SubCategoryID = sc.SubCategoryID
WHERE sc.SubcategoryID IN (SELECT s FROM Strings)
) x
GROUP BY CategoryId
HAVING COUNT(*) = (SELECT count(*) FROM Strings)
String-split copy from here. Note that you need a trailing ','.
Other implementations of string-split may be better for you, but this shows how to handle the 'ALL'-condition.