2

Here is my column looks like, named Sets.

My Column:

{C=Pass, D=Fail, E=Pass, F= Pass, G=Pass}
{C=Pass, D=Fail, E=NoApplication}

Expected

{C=Pass,  E=Pass, F= Pass, G=Pass}
{C=Pass,  E=NoApplication}

I wish to delete the "Fail" in my column. Please help.

Shnugo
  • 66,100
  • 9
  • 53
  • 114
wow5
  • 35
  • 4

2 Answers2

0

You may looking for Update Command

UPDATE TableName
SET Name = CASE WHEN CHARINDEX('=Fail,',Name ) > 1
                THEN STUFF(Name,CHARINDEX('=Fail,',Name)-1,LEN('=Fail,')+1,'')
                ELSE Name 
            END 
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
0

You can try this

UPDATE Use my approach within an updateable CTE

Changes the value in your table. Just replace @tbl with your actual table's name and `YourColumn with the column's name. Carefull with real data!

DECLARE @tbl TABLE(YourColumn VARCHAR(100));
INSERT INTO @tbl VALUES
 ('{C=Pass, D=Fail, E=Pass, F= Pass, G=Pass}')
,('{C=Pass, D=Fail, E=NoApplication}');

WITH Casted AS
(
    SELECT CAST('<x>' + REPLACE(SUBSTRING(t.YourColumn,2,LEN(YourColumn)-2),', ','</x><x>') + '</x>' AS xml) AsXml
            ,t.YourColumn AS OldValue
    FROM @tbl AS t
)
,UpdateableCTE AS
(
SELECT '{'
     + STUFF((
        SELECT ', ' + a.value('.','nvarchar(100)')
        FROM Casted.AsXml.nodes('/x') AS A(a)
        WHERE RIGHT(a.value('.','nvarchar(100)'),4)<>'Fail'
        FOR XML PATH('')
       ),1,2,'')
     + '}' AS NewValue
     ,OldValue
FROM Casted
)
UPDATE UpdateableCTE SET OldValue=NewValue;

SELECT * FROM @tbl;

The string is splitted into its elements. Then it is re-concatenated without the ones with "Fail".

Community
  • 1
  • 1
Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • How can I update the new column into my existing table? – wow5 Oct 20 '16 at 08:33
  • @wow5 I'm glad to read this! Pleaes allow me one tiny hint: Thx for the acceptance! If you don't mind it would be nice to - additionally - vote on answers. Since you've crossed the 15 rep points border yourself you are asked to vote. Votes are even more important than acceptance as they are counted for badges and privileges. – Shnugo Oct 20 '16 at 09:01