This is asked over and over in the forums, but why not proposing an 11g solution here
create table t(description varchar2(12) primary key,
numbers varchar2(4000));
insert into t(description, numbers) values ('PRIME','2,3,5,7');
insert into t(description, numbers) values ('ODD','1,3,5,7,9');
commit;
| DESCRIPTION |
NUMBERS |
| PRIME |
2,3,5,7 |
| ODD |
1,3,5,7,9 |
Now I want to unpivot numbers in rows
select description,(column_value).getnumberval()
from t,xmltable(numbers)
| DESCRIPTION |
(COLUMN_VALUE).GETNUMBERVAL() |
| PRIME |
2 |
| PRIME |
3 |
| PRIME |
5 |
| PRIME |
7 |
| ODD |
1 |
| ODD |
3 |
| ODD |
5 |
| ODD |
7 |
| ODD |
9 |
It is that simple
Works also with strings :
select (column_value).getstringval()
from xmltable('"a","b","c"');
| (COLUMN_VALUE).GETSTRINGVAL() |
| a |
| b |
| c |
11g, sql, xml
xml
Yesterday I had my first session about XML, today I have one about SQL Model
Ok, it was the first time I spoke about XML so I did not really now where to focus. XML is so big, you have XQUERY, XPATH, dozens of XML functions in the database.
One of the XML function is called XMLTRANSFORM and transforms XML according to XSLT
I had a fun demo about XSLT to create a semi-column separated list :
select
deptno,
xmltransform
(
sys_xmlagg
(
sys_xmlgen(ename)
),
xmltype
(
'<?xml version="1.0"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/ROWSET/ENAME">
<xsl:value-of select="text()"/>;</xsl:for-each>
</xsl:template>
</xsl:stylesheet>'
)
).getstringval() listagg
from emp
group by deptno;
DEPTNO LISTAGG
------- --------------------------------------
10 CLARK;KING;MILLER;
20 SMITH;FORD;ADAMS;SCOTT;JONES;
30 ALLEN;BLAKE;MARTIN;TURNER;JAMES;WARD;
sql, xml
xml