Search code examples
sql-serverxmlt-sqldml

How to delete node from XML column in DB


I am trying to remove a particular "node" from an XML column in my SQL Table. Below is an example of one of the XML column contents.

<GodBrandConfig>
  <AppSecret>hello</AppSecret>
  <WebClientUrl>url</WebClientUrl>
  <AllowableIpAddresses>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>.*</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
    <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
  </AllowableIpAddresses>
  <GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
</GodBrandConfig>

I am trying to delete duplicate records in here - For example " 178.160.245.88"

I have been trying many variations of the "delete" statement - Please can i have some assistance on this. set column.modify('delete /GodBrandConfig/AllowableIpAddresses/"178.160.245.88")') where idcolumn= 1125;


Solution

  • You need to select an AllowableIpAddress node in your XPath, and since you're wanting to remove a duplicate you can delete the second occurrence that matches the specified text using something like:

    update #DemoTable
    set [column].modify('delete /GodBrandConfig/AllowableIpAddresses/AllowableIpAddress[text()="178.160.245.88"][2]')
    where idcolumn = 1125;
    

    Which yields the updated XML:

    <GodBrandConfig>
        <AppSecret>hello</AppSecret>
        <WebClientUrl>url</WebClientUrl>
        <AllowableIpAddresses>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>.*</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>178.160.245.88</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
            <AllowableIpAddress>000.000.000.000</AllowableIpAddress>
        </AllowableIpAddresses>
        <GameplaySummaryUrl>about:blank</GameplaySummaryUrl>
    </GodBrandConfig>