Code Snippet for LIKE Search + SQL SERVER

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 334
LIKE search for filtering records for individual records as well as the combined (comma separated) IDs...

--Sample Data
Create table #temp(col1 varchar(100))
insert #temp values('1'), ('7'),('3'), ('4'), ('3,4'), ('3,4,7'), ('4,5,7')

DECLARE @filter VARCHAR(100) = '3,4'

--Your Query
SELECT * FROM #temp WHERE Col1 in (@filter)

/*
OUTPUT:
col1
3,4

*/

select * FROM #temp
where ',' + col1 + ',' LIKE '%,' + @filter + ',%'
OR ',' + @filter + ',' LIKE '%,' + col1 + ',%'

/*OUTPUT:

col1
3
4
3,4
3,4,7
*/

Comments or Responses

Login to post response