How to remove multiple nodes in XML using SQL

I have a column name XMLData that contains large XML (about 10,000 rows). Following is the type of stored XML.

<ABC>
  <DEF>
    <GHI>
      <JKL>value1</JKL>
      ..
    </GHI>
    <GHI>
      <JKL>value2</JKL>
      ..
    </GHI>
    <GHI>
      <JKL>value3</JKL>
      ..
    </GHI>
    ..
    ..
    <GHI>
      <JKL>valueN</JKL>
      ..
    </GHI>
    <OtherNodes>
    <OtherNodes1>
    .
    .
    .
  </DEF>
</ABC>

      

Is there a way to delete multiple nodes in a SQL query? Specifically, I want to remove all GHI nodes under Node DEF. Thank!!

+3


source to share


1 answer


For SQL Server, you just use modify

with delete

:

declare @x xml = '<ABC>
  <DEF>
    <GHI>
      <JKL>value1</JKL>
    </GHI>
    <GHI>
      <JKL>value2</JKL>
    </GHI>
    <GHI>
      <JKL>value3</JKL>
    </GHI>
    <GHI>
      <JKL>valueN</JKL>
    </GHI>
    <OtherNodes/>
    <OtherNodes1/>
  </DEF>
</ABC>'

set @x.modify('delete /ABC/DEF/GHI')

select @x

      



Result:

<ABC><DEF><OtherNodes /><OtherNodes1 /></DEF></ABC>

      

+2


source







All Articles