SET nocount ON
-- Declare a table and inserting some data with comma separated.
DECLARE
@MyTable TABLE (Value
VARCHAR(10))
INSERT
INTO @MyTable
VALUES('1,2,3')
INSERT INTO @MyTable VALUES('2,3,5')
INSERT
INTO @MyTable
VALUES('6,8,9')
-- Value to compared with comma separated column.
DECLARE @words VARCHAR(max) = '2,3,45,96'
DECLARE @split TABLE (word VARCHAR(64))
DECLARE @word VARCHAR(64),@start INT, @end INT, @stop INT
-- string split in 8 lines
SELECT @words += ',',@start = 1,@stop = Len(@words) + 1
WHILE @start < @stop
BEGIN
SELECT @end = Charindex(',', @words, @start),
@word = Rtrim(Ltrim(Substring(@words, @start, @end - @start))),
@start = @end + 1
INSERT @split VALUES (@word)
END
-- This
will split the comma separated value like this:
-- Select the Records that matches the given crieteia.
SELECT *
FROM @split
SELECT *
FROM @MyTable a
WHERE EXISTS (SELECT *
FROM @split w
WHERE Charindex(',' + w.word + ',', ',' + a.Value + ',') > 0)
Output:

vidya shrungarePosted Sep 29, 2018, 1:43 AM
How i can highlight 2 and 3 on specific condition
anil babuPosted Feb 25, 2014, 5:28 AM
Give me stored procedure using sql server,Table1: Id FullName ------------------- 1 Cleo,Smith,james Table2: I want to separate the comma delimited string into 3 columns Id FullName Name Surname Last --- ------------ ------- ----------- ------ 1 Cleo,Smith Cleo Smith james First table values separate comma vles after that values insert into Table2 ?
Ravi ShekharPosted May 31, 2013, 4:42 AM
Nice%20blog%20sir