I have the below update statement which works most of the time, but in the below example, the text White
is not removed in line 2.
What needs to be changed in the below syntax so that the text from field2
is ALWAYS removed if it exists in field1
regardless of the text position in the string?
Declare @Table1 Table (field1 varchar(100), field2 varchar(100))
Insert Into @Table1 (field1, field2) Values
('Blue Green', 'Green'), ('Red White', 'Socks Are White')
UPDATE @Table1
SET field2 = REPLACE(field1,field2,'')
WHERE field1 like '%'+field2+'%';
Select * from @Table1
EDIT
Expected result after update is
'Blue Green', ''
'Red White', 'Socks Are'
Omit the word Green
from field2 since it exists in field1 and omit the word White
from field2 since it exists in field1