I think that in the interest of validity of data, it should be normalized so that you split the Items into a separate table with an item on each row.
In either case, here is a working sample that uses a user defined function to split the incoming string into a Table Variable and then uses JOIN
with a LIKE
CREATE FUNCTION dbo.udf_ItemParse
(
@Input VARCHAR(8000),
@Delimeter char(1)='|'
)
RETURNS @ItemList TABLE
(
Item VARCHAR(50) ,
Pos int
)
AS
BEGIN
DECLARE @Item varchar(50)
DECLARE @StartPos int, @Length int
DECLARE @Pos int
SET @Pos = 0
WHILE LEN(@Input) > 0
BEGIN
SET @StartPos = CHARINDEX(@Delimeter, @Input)
IF @StartPos < 0 SET @StartPos = 0
SET @Length = LEN(@Input) - @StartPos - 1
IF @Length < 0 SET @Length = 0
IF @StartPos > 0
BEGIN
SET @Pos = @Pos + 1
SET @Item = SUBSTRING(@Input, 1, @StartPos - 1)
SET @Input = SUBSTRING(@Input, @StartPos + 1, LEN(@Input) - @StartPos)
END
ELSE
BEGIN
SET @Pos = @Pos+1
SET @Item = @Input
SET @Input = ''
END
INSERT @ItemList (Item, Pos) VALUES(@Item, @Pos)
END
RETURN
END
GO
DECLARE @Itemstable TABLE
(
ItemId INT,
Items VarChar (1000)
)
INSERT INTO @Itemstable
SELECT 1 itemID, 'school,college' items UNION
SELECT 2, 'place, country' UNION
SELECT 3, 'college,cricket' UNION
SELECT 4, 'School,us,college' UNION
SELECT 5, 'cricket,country,place' UNION
SELECT 6, 'footbal,tenis,place' UNION
SELECT 7, 'names,tenis,cricket' UNION
SELECT 8, 'sports,tenis'
DECLARE @SearchParameter VarChar (100)
SET @SearchParameter = 'cricket'
SELECT DISTINCT ItemsTable.*
FROM @Itemstable ItemsTable
INNER JOIN udf_ItemParse (@SearchParameter, ',') udf
ON ItemsTable.Items LIKE '%' + udf.Item + '%'
SET @SearchParameter = 'cricket,tenis'
SELECT DISTINCT ItemsTable.*
FROM @Itemstable ItemsTable
INNER JOIN udf_ItemParse (@SearchParameter, ',') udf
ON ItemsTable.Items LIKE '%' + udf.Item + '%'