find missing word in comma delimited string by comparing with another string

given two tables with columns like so:
TableA
id Col1
1 blue,green,pink
2 fat,ugly,stupid,smart
3 larry,james,philip,john

TableB
id Col2
3  larry,james,tih,john,ann,,
2 fat,krank,ugly,stupid,smart
1 blue,green,pink,yellow

I want to be able to find and add (append) to TableB (col2) all the missing words found in TableA, Col1 using the id as the relationship that link both tables.  The end result should be Col1 a subset of Col2. words are comma seperated.
peterslove53Asked:
Who is Participating?
 
peterslove53Author Commented:
i got the answer using the DelimitedSplit8K function, found here
http://www.sqlservercentral.com/Forums/Topic1518039-8-1.aspx


DECLARE @String1 varchar(30) = '356,840',
        @String2 varchar(30) = '840,356'

IF NOT EXISTS(
            SELECT fn.Item from dbo.DelimitedSplit8K(@String1,',') fn
            EXCEPT
            SELECT fn.Item from dbo.DelimitedSplit8K(@String2,',') fn
            )
            PRINT 'No Differences found'
ELSE
            PRINT 'they are not the same, regardless of order'
0
 
Scott PletcherSenior DBACommented:
I thought you wanted to update tableb with the missing value(s)?  Sorry I spent time on it if you don't really need that:


IF OBJECT_ID('tempdb..#missing_values') IS NOT NULL
    DROP TABLE #missing_values
CREATE TABLE #missing_values (
    id int,
    value varchar(500)    
    )
CREATE CLUSTERED INDEX #missing_values__CL ON #missing_values ( id, value )

INSERT INTO #missing_values
SELECT a.id, a_list.item
FROM TableA a
CROSS APPLY dbo.DelimitedSplit8K(a.Col1, ',') AS a_list
LEFT OUTER JOIN (
    SELECT b.id, b_list.item
    FROM TableB b
    CROSS APPLY dbo.DelimitedSplit8K(b.Col2, ',') AS b_list
    WHERE
        b_list.item > ''
) AS b_split ON
    b_split.id = a.id AND
    b_split.item = a_list.item
WHERE
    a_list.item > '' AND
    b_split.id IS NULL
ORDER BY id, item
   
--SELECT 'TableA' AS table_name, * FROM TableA
--SELECT 'TableB' AS table_name, * FROM TableB
--SELECT '#Missing_values' AS table_name, * FROM #missing_values

UPDATE TableB
SET Col2 = Col2 + missing.[values]
FROM TableB b
INNER JOIN (
    SELECT ids.id, (SELECT ',' + value FROM #missing_values mv WHERE mv.id = ids.id ORDER BY value FOR XML PATH('')) AS [values]
    FROM (
        SELECT DISTINCT id
        FROM #missing_values
    ) AS ids
) AS missing ON
    missing.id = b.id

--SELECT * FROM TableB
0
 
peterslove53Author Commented:
thank you ScottPletcher, that is even more handy. I was doing it a long and expensive way.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
peterslove53Author Commented:
Hi ScottPletcher, i implemented your code above but i am getting a syntax error.  
"    Incorrect syntax near '.'     "

and it is from this line
CROSS APPLY dbo.DelimitedSplit8K(b.srch_string, ',') AS b_list
0
 
Scott PletcherSenior DBACommented:
You didn't select my response as any part of your answer.  Good luck with your implementation errors.
0
 
peterslove53Author Commented:
i figured out why i was getting the error. it has nothing to do with the code. my database not operate in Compatibility mode 90 or above. Cant change that bc of other applications. i ll have to find another way of solving the problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.