Concatenate XMLType nodes in Oracle query

后端 未结 1 1184
轮回少年
轮回少年 2021-01-26 20:58

I have a CLOB column that contains XML type data. For example XML data is:

123456789
         


        
1条回答
  •  青春惊慌失措
    2021-01-26 21:13

    The concat() SQL function concatenates two values, so it's just appending the semicolon to each extracted value independently. But you're really trying to do string aggregation of the results (which could, presumably, really be more than two extracted values).

    You can use XMLQuery instead of extract, and use an XPath string-join() function to do the concatentation:

    XMLQuery('string-join(/A/B, ";")' passing xmltype(a.xml) returning content)
    

    Demo with fixed XMl end-node tags:

    -- CTE for sample data
    with a (xml) as (
      select '123456789' from dual
    )
    -- actual query
    select XMLQuery('string-join(/A/B, ";")' passing xmltype(a.xml) returning content) as result
    from a;
    
    RESULT
    ------------------------------
    123;789
    

    You could also extract all of the individual values using XMLTable, and then use SQL-level aggregation:

    -- CTE for sample data
    with a (xml) as (
      select '123456789' from dual
    )
    -- actual query
    select listagg(x.b, ';') within group (order by null) as result
    from a
    cross join XMLTable('/A/B' passing xmltype(a.xml) columns b number path '.') x;
    
    RESULT
    ------------------------------
    123;789
    

    which gives you more flexibility and would allow grouping by other node values more easily, but that doesn't seem to be needed here based on your example value.

    0 讨论(0)
提交回复
热议问题