Avatar of Southern_Gentleman
Southern_Gentleman
Flag for United States of America asked on

Remove items in csv string

I was able to use the following query from Jim Horn Single column to normalize a comma string into a single column but then I did a NOT EXISTS from another table to find out what values need to be removed.

So now that i have that that information, how would i remove the items that i've found and go to the original CSV string and remove the one item or items that should not belong.

So, i have to figure out how to remove all the possible combinations of commas as well as well as the invalid item.
Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Vitor Montalvão

Provide us sample data so we know what do deal with.
Southern_Gentleman

ASKER
ok, here is a sample of what we are working with.

Table 1

customers      |      products
1                    |      aaa,bbb,ccc
2                    |      ddd,eee,fff
3                    |      bbb,ccc,ggg
4                    |      ppp,sss,ttt
5                     |      fff,qqq,mmm
6                    |      ooo,nnn,ddd


Table 2 (NOT VALID ITEMS LIST Or NOT EXISTS Results (insert into a temp table))

Customers      |      Products
1                    |      bbb
3                    |      ccc
4                    |      ppp


--Query to do the following

Results should be deleting the CSV value from table 2 products from the products in table1 since they are no longer valid
customers      |      products
1                    |      aaa,ccc
2                    |      ddd,eee,fff
3                    |      bbb,ggg
4                    |      sss,ttt
5                    |      fff,qqq,mmm
6                    |      ooo,nnn,ddd

So you can see that we removed 'bbb' for customer 1 and 'ccc' for customer 3  and 'ppp' for customer 4 from the actual csv string.
Vitor Montalvão

My suggestion is to use the following solution:
UPDATE t1
SET t1.products = REPLACE(t1.products,t2.products,'')
FROM table1 t1
	INNER JOIN table2 t2 ON t1.customers = t2.customers

Open in new window

This will remove those products in table1 but will let double commas (when removed from the middle of the string), initial commas (where removing from the beginning of the string and final commas (when removing from the end of the string) so you'll need to run the following update to sort out this situation:
UPDATE table1
SET products = CASE
		WHEN LEFT(products,1)=',' THEN RIGHT(products,LEN(products)-1)
		WHEN RIGHT(products,1)=',' THEN LEFT(products,LEN(products)-1)
		ELSE REPLACE(products,',,',',')
	END

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Scott Pletcher

Careful, that could destroy your data!
Southern_Gentleman

ASKER
Thanks guys, i'm still testing this. the only thing i forgot was that the original string had spaces after the commas so the case when is missing some when statements

customers      |      products
1                    |      aaa, bbb, ccc
2                    |      ddd, eee, fff
3                    |      bbb, ccc, ggg
4                    |      ppp, sss, ttt
5                     |      fff, qqq, mmm
6                    |      ooo, nnn, ddd
Vitor Montalvão

You just need to add the space in the REPLACE function in the ELSE statement and consider 2 characters for the other situations:
UPDATE table1
SET products = CASE
	WHEN LEFT(products,2)=', ' THEN RIGHT(products,LEN(products)-2)
	WHEN RIGHT(products,2)=' ,' THEN LEFT(products,LEN(products)-2)
	ELSE REPLACE(products,', ,',',')
	END

Open in new window

NOTE: Do not run this in your Production environment before testing it in a Development environment.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Southern_Gentleman

ASKER
so i'm still analyzing this and for some reason in my live table it will remove the first results from my update query but won't update the other items that meet my criteria from the temp table.

UPDATE dbo.livetable
SET product = REPLACE(t1.product,t2.product,'')
FROM dbo.livetable as t1
	INNER JOIN #update t2 ON t1.customers = t2.customers AND t1.location = t2.location AND t1.programId = t2.programid

Open in new window

ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.