I have a SQL table with an xml column that has values like this, across multiple rows:
<array>
<foo>
<property name="Name">Foo 1</property>
<property name="Gender">M</property>
<property name="DOB">2020-01-01</property>
</foo>
<foo>
<property name="Name">Foo 2</property>
<property name="Gender">M</property>
<property name="DOB">2020-01-02</property>
</foo>
<foo>
<property name="Name">Foo 3</property>
<property name="Gender">F</property>
<property name="DOB">2020-01-03</property>
</foo>
</array>
I'd like to write a SQL statement that can update all occurrences of the property["Name"]
value to 1
(remove the Foo
).
I've been trying SQL such as:
select xmlcolumn.value('/array/foo[@name="Name"]/', 'nvarchar(max)')
from xmltable
and a few variations of that XML query, but haven't had luck in figuring it out. The result should be a table with XML values that look like this (note the property with the Name attribute is set to 1 for all occurrences):
<array>
<foo>
<property name="Name">1</property>
<property name="Gender">M</property>
<property name="DOB">2020-01-01</property>
</foo>
<foo>
<property name="Name">1</property>
<property name="Gender">M</property>
<property name="DOB">2020-01-02</property>
</foo>
<foo>
<property name="Name">1</property>
<property name="Gender">F</property>
<property name="DOB">2020-01-03</property>
</foo>
</array>