We help IT Professionals succeed at work.

Remove items in csv string

43 Views
Last Modified: 2017-04-21
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.
Comment
Watch Question

Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

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

Author

Commented:
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ãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Careful, that could destroy your data!

Author

Commented:
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ãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.

Author

Commented:
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

IT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION