Have a look at example:
DECLARE @tmp VARCHAR(30)
DECLARE @t TABLE (RowNo INT, MyValue VARCHAR(10), Remainder VARCHAR(30))
SET @tmp = 'A,B,C,D,A,E,C'
;WITH MyCTE AS
(
SELECT 1 AS RowNo, LEFT(@tmp, CHARINDEX(',',@tmp)-1) AS MyValue, RIGHT(@tmp, LEN(@tmp) - CHARINDEX(',',@tmp)) AS Remainder
WHERE CHARINDEX(',',@tmp)>0
UNION ALL
SELECT RowNo +1 AS RowNo, LEFT(Remainder, CHARINDEX(',',Remainder)-1) AS MyValue, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',',Remainder)) AS Remainder
FROM MyCTE
WHERE CHARINDEX(',',Remainder)>0
UNION ALL
SELECT RowNo +1 AS RowNo, Remainder AS MyValue, NULL AS Remainder
FROM MyCTE
WHERE CHARINDEX(',',Remainder)=0
)
INSERT INTO @t (RowNo, MyValue, Remainder)
SELECT RowNo, MyValue, Remainder
FROM MyCte
SELECT *
FROM @t
SELECT MyValue, COUNT(MyValue) AS CountOfMyValue
FROM @t
GROUP BY MyValue
HAVING COUNT(MyValue)>=1
In above example
Common Table Expression[
^] has been used to split field values into set of records.
You did not provide enough information to help you more. Please, improve your question and i'll promise to update my answer to show you how to remove duplicates (update field).
[EDIT]
I'm glad on update the question. The final solution is...
DECLARE @srctbl TABLE (id INT IDENTITY(1,1), names VARCHAR(50))
INSERT INTO @srctbl VALUES ('abc,jkm,pqr,abc,def,ghi,jkm,ghi'),
('mno,pqr,abc,mno,pqr'), ('abc,jkm,stu,jkm,rcm,tin')
DECLARE @dsttbl TABLE (id INT, RowNo INT, MyValue VARCHAR(10), Remainder VARCHAR(30))
;WITH MyCTE AS
(
SELECT id, 1 AS RowNo, LEFT(names, CHARINDEX(',',names)-1) AS MyValue, RIGHT(names, LEN(names) - CHARINDEX(',',names)) AS Remainder
FROM @srctbl
WHERE CHARINDEX(',',names)>0
UNION ALL
SELECT id, RowNo +1 AS RowNo, LEFT(Remainder, CHARINDEX(',',Remainder)-1) AS MyValue, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',',Remainder)) AS Remainder
FROM MyCTE
WHERE CHARINDEX(',',Remainder)>0
UNION ALL
SELECT id, RowNo+1 AS RowNo, Remainder AS MyValue, NULL AS Remainder
FROM MyCTE
WHERE CHARINDEX(',',Remainder)=0
)
INSERT INTO @dsttbl (id, RowNo, MyValue, Remainder)
SELECT id, RowNo, MyValue, Remainder
FROM MyCte
UPDATE t3 SET t3.names = t4.names
FROM @srctbl AS t3 INNER JOIN (
SELECT t1.id, STUFF( (SELECT ',' + MyValue AS 'text()'
FROM (
SELECT id, MyValue
FROM @dsttbl
GROUP BY id, MyValue
HAVING COUNT(MyValue)>=1
) AS t2
WHERE t2.id = t1.id
FOR XML PATH('')), 1, 1, '') AS [names]
FROM @dsttbl AS t1
GROUP BY t1.id
) AS t4 ON t3.id = t4.id
SELECT *
FROM @srctbl
[/EDIT]