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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.