Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a table column i've data as given below

<PivotSet>
<item>
<column name = "RNO">1</column>
<column name = "MIN(INSGRP)">Record- 001</column>
</item>
<item>
<column name = "RNO">2</column>
<column name = "MIN(INSGRP)">Record- 002</column>
</item>
<item>
<column name = "RNO">3</column>
<column name = "MIN(INSGRP)">Record- 003</column>
</item>
<item>
<column name = "RNO">4</column>
<column name = "MIN(INSGRP)">Record- 004</column>
</item>
<item>
<column name = "RNO">5</column>
<column name = "MIN(INSGRP)">Record- 005</column>
</item>
<item>
<column name = "RNO">6</column>
<column name = "MIN(INSGRP)">Record- 006</column>
</item>
</PivotSet>">

so i want to generate six columns into new table from this column data given above. this is single column data of a row. Each row hold this kind of column of xml string.

Columns may be dynamic i.e. may be other 1 or 2 or .....

Result should be shown as below
RNO MIN(INSGRP)
1 Record- 001
2 Record- 002
3 Record- 003
4 Record- 004
5 Record- 005
6 Record- 006

What I have tried:

so i want to generate six columns into new table from this column data given above. this is single column data of a row. Each row hold this kind of column of xml string and may have different string.
Posted
Updated 11-Jan-17 19:33pm
v6
Comments
Afzaal Ahmad Zeeshan 11-Jan-17 9:34am    
You need to use some sort of Xml Parser. There are many available.

1 solution

You cab use XMLTABLE to fetrch values from the XML. If the data is in varchar, change it first to XMLTYPE in the query.

Consider the following example

Create the test table
SQL
CREATE TABLE XmlTestData (
   TheData varchar2(2000)
);

Add the test row
INSERT INTO XmlTestData (TheData)
VALUES ('<PivotSet>
<item>
<column name = "RNO">1</column>
<column name = "MIN(INSGRP)">Record- 001</column>
</item>	
<item>
<column name = "RNO">2</column>
<column name = "MIN(INSGRP)">Record- 002</column>
</item>
<item>
<column name = "RNO">3</column>
<column name = "MIN(INSGRP)">Record- 003</column>
</item>
<item>
<column name = "RNO">4</column>
<column name = "MIN(INSGRP)">Record- 004</column>
</item>
<item>
<column name = "RNO">5</column>
<column name = "MIN(INSGRP)">Record- 005</column>
</item>
<item>
<column name = "RNO">6</column>
<column name = "MIN(INSGRP)">Record- 006</column>
</item>
</PivotSet>');

COMMIT;

Run the query which uses the following logic:

- select the data from the table
- extra each item as separate row
- extract the columns from a single item

SQL
SELECT TO_NUMBER(extractvalue(res.RNO, 'text()'))      AS RNO,
       TO_CHAR(extractvalue(res.MIN_INSGRP, 'text()')) AS MIN_INSGRP
FROM XmlTestData xtd,
     XMLTABLE('/PivotSet/item'
              PASSING XMLTYPE(xtd.TheData) 
              COLUMNS XmlItem XMLTYPE PATH 'column' 
              ) items,
     XMLTABLE('/'
              PASSING items.XmlItem 
              COLUMNS 
                 RNO        XMLTYPE PATH 'column[@name="RNO"]/text()',
                 MIN_INSGRP XMLTYPE PATH 'column[@name="MIN(INSGRP)"]/text()'
              ) res;

The result is
RNO   MIN_INSGRP
---   ----------
1     Record- 001
2     Record- 002
3     Record- 003
4     Record- 004
5     Record- 005
6     Record- 006
 
Share this answer
 
Comments
Maciej Los 11-Jan-17 15:35pm    
Well explained!
5.
Wendelius 12-Jan-17 8:50am    
Thanks!
.NET- India 12-Jan-17 7:09am    
Thanks for your ans. but i want it in columns as shown above.
Wendelius 12-Jan-17 8:14am    
Well, yesterday your output example was different, the same as in my answer...

If you need to have the data in separate columns then the amount of columns cannot be dynamic. So you basically need to decide how many columns you have in the result set before you run the query. The amount of columns cannot be different on different rows.
Wendelius 12-Jan-17 8:36am    
I restored the original version of the question.

If needed make the additions to the desired output, but also include the definitions how the amount of columns should be decided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900