I get a table type variable like this
color
-------------
Black
Blue<br/>PMS 285C
Green<br/>PMS 360
Red<br/>PMS 199
I want to make them like
color
-------------
Black, Blue, Green, Red
But I am having a very difficult time to remove the "<.." stuff. I followed this answer: Is there a way to loop through a table variable in TSQL without using a cursor? To create a temp table to loop and remove the characters, but still does not work
DECLARE @result int, @i int = 0, @colors varchar(800);
DECLARE @results table(color varchar(40));
INSERT INTO @results SELECT color FROM...
DECLARE @temp table(Id INT, color varchar(40));
INSERT INTO @temp SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Id, color FROM @results
WHILE (SELECT Count(*) FROM @temp WHERE color like '%<%') > 0
BEGIN
SELECT Top 1 @i = Id FROM @temp WHERE color like '%<%'
UPDATE @temp Set color = (case when color like '%<%'
then left(color, charindex('<', color) - 1)
else color
end) Where Id = @i
Delete @temp Where Id = @i
END
I still get
color
------------------------------------------------------------------------------
Black, Blue<br/>PMS 285C, Green<br/>PMS 360, Red<br/>PMS 199
Could anyone help me please? Or give some suggestions.
Thanks