I have a SQL table with a XML column.
Table structure is
CREATE TABLE [dbo].[UserSettings](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[UserName] [varchar](100) NULL,
[Setting] [xml] NULL,
[CreateDate] [datetime] NOT NULL
)
The Setting xml column looks like this:
<UserSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserID>1</UserID>
<Reports>
<string>Report - 1</string>
<string>Report - 2</string>
<string>Report - 3</string>
<string>Report - 4</string>
</Reports>
</UserSettings>
I want to update Report - 1 to Report - One, Report - 2 to Report - Two in <Reports>
And also, if there is a value Report - 4 then I want to remove it from <Reports>
So my end result for in the column should look like this:
<UserSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserID>1</UserID>
<Reports>
<string>Report - One</string>
<string>Report - Two</string>
<string>Report - Three</string>
</Reports>
</UserSettings>
EDIT: I have tried the REPLACE approach. I am not sure if that is the most effective way to do this.
update UserSettings
set Setting=REPLACE(convert(varchar(max),Setting),'<string>Report - 1</string>','<string>Report - One</string>')
where convert(varchar(max),Setting) like '%<string>Report - 1</string>%'
EDIT: I read about modify() method but the sequence of report could be in any order. I couldn't get my query to work with it.