Converting each row in a column to an XML
Hi everyone,
Using Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit
I have a table where there is a comments column which is VARCHAR2. That column contains string data in the form of XML tags.
Like for eg. *<tag_name attr_name="10"/>* (sorry cant provide the exact values as I'm not allowed to)
There are many such rows. Now I want to use these strings and operate on them as XML like using XPath to find certain attributes etc.
I tried using the extract function and to use it i need a XMLType object. So I tried to create one using the XMLType() constructor.
But it gives me the error ORA-19032: Expected XML tag , got no content
When I looked it up on the net, I found that this error number doesn't correspond to the error message that I'm getting. The error msg found on the internet was Expected XML tag string got string
I used this query
SELECT XMLType (<column>) FROM <table>;But when I modified it (based on someone's hunch) to
SELECT XMLType (<column>)
FROM <table>
WHERE ROWNUM <= 1;The query worked fine. Like it can take only one row.
I also tried like this
SELECT XMLType ('<tag_name attr_name="10"/>')
FROM DUAL;This too worked fine.
So finally this has left me completely confused. Is there a way to convert every row in a column to an XMLType object so that I can use the extract function to gather information about it.
For the time being I have used REGEXes to write the code. But having the flexibility of xml would be better.
Any help would be appreciated.
Regards,
Arijit
Edited by: Arijit Kanrar on May 23, 2013 5:27 AM
Arijit,
The error message is correct. What you found is merely the generic message with string placeholders.
The error message is also pretty self-explanatory : you can't pass an empty string (NULL) to the XMLType constructor.
SQL> select xmltype('') from dual;
ERROR:
ORA-19032: Expected XML tag , got no content
ORA-06512: at "SYS.XMLTYPE", line 310
ORA-06512: at line 1
no rows selectedYou have to either add a WHERE clause to filter out NULL columns, or use a CASE statement to only convert strings that aren't empty :
SELECT XMLType (<column>)
FROM <table>
WHERE <column> IS NOT NULL ;
SELECT CASE WHEN <column> IS NOT NULL THEN XMLType (<column>) END
FROM ...And do not use EXTRACT if you want to access scalar values, use EXTRACTVALUE instead :
SQL> select extractvalue(xmltype('<tag_name attr_name="10"/>'), '/tag_name/@attr_name') from dual;
EXTRACTVALUE(XMLTYPE('<TAG_NAMEATTR_NAME="10"/>'),'/TAG_NAME/@ATTR_NAME')
10
Similar Messages
-
Convert one row to multiple column dynamic in smartform
i want to convert on row to multiple column in smartform.As number of column is not per define..Please suggest the way out in smartform
I saw a post and working perfect. The link is given below.
http://scn.sap.com/community/abap/blog/2013/10/06/the-case-of-dynamic-columns-in-smartform
Thanks to Eitan. -
Is there a way to convert 8 Rows by 2 Columns to 2 Rows by 8 Columns?
I have tried but came to an stop...
How do I convert 8 Rows by 2 Columns to 2 Rows by 8 Columns?
as shown in the vi attached
Thank you very much...
Solved!
Go to Solution.
Attachments:
Eight Rows Two Col convert to two Rows Eight Col.vi 8 KBJohnMc19 wrote:
Hi Diego2000,
Trying using the Transpose 2D array function found in the Array functions palette, think it is what you are asking for.
Regards,
No, for the required transformation reshape array is required, not transpose.
Compare:
Andrey. -
How to convert a row into a column with the row headers displayed as column in javaFx?
How do in convert a row of data into column of data to display as shown below:
Column1|Column2|Column3|Column4
C1 | C2 | C3 | C4
C5 | C6 | C7 | C8
How to convert the above default behavior to as below
Column1| C1 | C5
Column2| C2 | C6
Column3| C3 | C7
Column4| C4 | C8.
-
Trying to convert multiple rows into multipe columns within a single row
I am trying to convert data from multiple rows into multiple columns. Let me see if I can paint the picture for you.
Here is a sample of the table i am trying to read from:
Company Name Account
1 Sam 123
1 Sam 234
1 Joe 345
1 Sue 789
1 Sue 987
1 Sue 573
I am trying to put this into a View that would have the data represented as such:
Company Name Acct1 Acct2 Acct3 Acct4
1 Sam 123 234 <null> <null>
1 Joe 345 <null> <null> <null>
1 Sue 789 987 573 <null>
Many thanks in advance for your help!test@XE> --
test@XE> with t as (
2 select 1 as company, 'Sam' as name, 123 as account from dual union all
3 select 1, 'Sam', 234 from dual union all
4 select 1, 'Joe', 345 from dual union all
5 select 1, 'Sue', 789 from dual union all
6 select 1, 'Sue', 987 from dual union all
7 select 1, 'Sue', 573 from dual)
8 --
9 select company,
10 name,
11 max(case when rn = 1 then account else null end) as acct1,
12 max(case when rn = 2 then account else null end) as acct2,
13 max(case when rn = 3 then account else null end) as acct3,
14 max(case when rn = 4 then account else null end) as acct4
15 from (select company,
16 name,
17 account,
18 row_number() over (partition by company, name order by 1) as rn
19 from t)
20 group by company, name;
COMPANY NAM ACCT1 ACCT2 ACCT3 ACCT4
1 Joe 345
1 Sam 234 123
1 Sue 573 789 987
3 rows selected.
test@XE>
test@XE>isotope -
Convert 1 row, multiple (identical) columns into 1 column, multiple rows
I have 10g, so unpivot is unavailable to me. I have a query that looks like:
with data as (
select 1,2,3 from dual
) select * from data
which returns 1,2,3. I need it to return:
1
2
3 and be able to name the output column. The results of this will be used in a sub-select in an IN clause. This is a trivial example. In reality the list of numbers will be varied and large - too many to either use bind variables or to put in the IN clause as a list. Is there a simple way I'm overlooking?Hi,
One way to unpivot is to join to a table that has as many rows as you need. Instead of an actual table, you can always generate a Counter Table (actually a result set), with exactly the right number of rows. Use CASE (or something similar) go get a different column for each row of the counter table.
For example, to get the 3 columns of the scott.dept table onto 3 separate rows:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT CASE c.n
WHEN 1 THEN TO_CHAR (d.deptno)
WHEN 2 THEN d.dname
WHEN 3 THEN d.loc
END AS data
FROM scott.dept d
CROSS JOIN cntr c
;Output:
DATA
10
20
30
40
ACCOUNTING
RESEARCH
SALES
OPERATIONS
NEW YORK
DALLAS
CHICAGO
BOSTON -
Convert different rows into single column
DB : 11.1.0.7
OS : Solaris Sparc 5.10
I have one query which is joining few tables and giving me output like below.
personnum orgnm
======= =======
6 Keyholder
9 Sales
3 Mgmt
I would like to convert that into single column like below.
col1
========
6,Keyholder,9,Sales,3,Mgmt
I have tried with pivot and decode, but not getting resule which I am exepcting. Any suggesstions ?yashwanth437 wrote:
listagg() function could work.LISTAGG is not available in 11.1. It was introduced in 11.2.
Anyway, XML solution:
with sample_table as (
select 6 personnum,'Keyholder' orgnm from dual union all
select 9,'Sales' from dual union all
select 3,'Mgmt' from dual
select rtrim(xmlagg(xmlelement(e,personnum || ',' || orgnm,',').extract('//text()')),',') col1
from sample_table
COL1
6,Keyholder,9,Sales,3,Mgmt
SQL> SY. -
How to convert some row values in columns
Hello guys,
I'm using Oracle 10g in a windows platform, I have the following data:
with test as
select '000-ME-001' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
select '000-ME-001' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
select '000-ME-001' tag_number, 'Capacity 350' tag_details1, '000-TS-M-228' tag_details2 from dual union all
select '000-ME-002' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
select '000-ME-002' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
select '000-ME-002' tag_number, 'Capacity 350' tag_details1, '000-TS-M-228' tag_details2 from dual union all
select '000-ME-003' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
select '000-ME-003' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
select '000-ME-003' tag_number, 'Capacity 350' tag_details1, '' tag_details2 from dual
)How can I covert the values of tag_details1 as column name to achieve this result:
tag_number Capacity 150 Capacity 250 Capacity 350
000-ME-001 000-TS-M-226 000-TS-M-227 000-TS-M-228
000-ME-002 000-TS-M-226 000-TS-M-227 000-TS-M-228
000-ME-003 000-TS-M-226 000-TS-M-227Hope you can help me, best regards.select tag_number
, max (decode (tag_details1, 'Capacity 150', tag_details2))
, max (decode (tag_details1, 'Capacity 250', tag_details2))
, max (decode (tag_details1, 'Capacity 350', tag_details2))
from test
group by tag_numberas in
SQL> with test as
2 (
3 select '000-ME-001' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
4 select '000-ME-001' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
5 select '000-ME-001' tag_number, 'Capacity 350' tag_details1, '000-TS-M-228' tag_details2 from dual union all
6 select '000-ME-002' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
7 select '000-ME-002' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
8 select '000-ME-002' tag_number, 'Capacity 350' tag_details1, '000-TS-M-228' tag_details2 from dual union all
9 select '000-ME-003' tag_number, 'Capacity 150' tag_details1, '000-TS-M-226' tag_details2 from dual union all
10 select '000-ME-003' tag_number, 'Capacity 250' tag_details1, '000-TS-M-227' tag_details2 from dual union all
11 select '000-ME-003' tag_number, 'Capacity 350' tag_details1, '' tag_details2 from dual
12 )
13 select tag_number
14 , max (decode (tag_details1, 'Capacity 150', tag_details2))
15 , max (decode (tag_details1, 'Capacity 250', tag_details2))
16 , max (decode (tag_details1, 'Capacity 350', tag_details2))
17 from test
18 group by tag_number
19 /
TAG_NUMBER MAX(DECODE(T MAX(DECODE(T MAX(DECODE(T
000-ME-002 000-TS-M-226 000-TS-M-227 000-TS-M-228
000-ME-001 000-TS-M-226 000-TS-M-227 000-TS-M-228
000-ME-003 000-TS-M-226 000-TS-M-227Edited by: Alex Nuijten on Jun 3, 2009 4:03 PM -
Convert the row in to column which has 2 row
Hi All
i have a result set like
prd_d
2011
2012
what i need is
prd_d_one prd_d_two
2011 2012
thanksYou can use PIVOT:
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
More likely you need dynamic (data-driven) PIVOT not hard-wired like above:
http://www.sqlusa.com/bestpractices2005/dynamicpivot/
Kalman Toth Database & OLAP Architect
SELECT Video Tutorials 4 Hours
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
Convert table row data into column - 8i
hello all,
i have a table with the following data
Machine Time
MAC1 - 1
MAC2 - 2
MAC3 - 3
MAC4 - 4
MAC5 - 5
and want to have a query as only for the mac1, mac2 and mac3
MAC1 MAC2 MAC3
1 2 3
how to acheived this...i am on 8i thanks in adv.
KKSELECT SUM(DECODE(MACHINE,'MAC1',TIME)) "MAC1",
SUM(DECODE(MACHINE,'MAC2',TIME)) "MAC2" ,
SUM(DECODE(MACHINE,'MAC3',TIME)) "MAC3" FROM TABLENAME -
CREATE TABLE #ids (empid VARCHAR(200))
INSERT INTO #ids SELECT '100,200,300,400'
INSERT INTO #ids SELECT '1100,1200,1300,1400'
I am trying to get output following format
ID_1 ID_2 ID_3 ID_4
100 200 300 400
1000 1200 1300 1400
Each row in EmpId column will have only max. four values
Thanks
VOne more method:
CREATE TABLE #ids (empid VARCHAR(200))
INSERT INTO #ids SELECT '100,200,300,400'
INSERT INTO #ids SELECT '1100,1200,1300,1400'
select * from #ids
;WITH
L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
L1 AS(SELECT 1 AS c FROM L0 AS A, L0 AS B),
L2 AS(SELECT 1 AS c FROM L1 AS A, L1 AS B),
L3 AS(SELECT 1 AS c FROM L2 AS A, L2 AS B),
Numbers AS(SELECT ROW_NUMBER() OVER(ORDER BY c) AS n FROM L3)
SELECT [1] AS Column1, [2] AS Column2, [3] AS Column3, [4] AS Column4
FROM
(SELECT rn,
ROW_NUMBER() OVER (PARTITION by rn ORDER BY nums.n) AS PositionInList,
LTRIM(RTRIM(SUBSTRING(valueTable.empid, nums.n,
charindex(N',', valueTable.empid + N',', nums.n) - nums.n))) AS [Value]
FROM Numbers AS nums INNER JOIN (Select row_number()over (Order by (Select NULL)) rn , Empid From #ids) AS valueTable ON nums.n <= CONVERT(int, LEN(valueTable.empid))
AND SUBSTRING(N',' + valueTable.empid, n, 1) = N',') AS SourceTable
PIVOT
MAX([VALUE]) FOR PositionInList IN ([1], [2], [3], [4])
) AS Table2
drop table #ids -
How to exclude the XML declaration from each row of the result set?
Hi,
I have a table with an XMLTYPE column and would like to SELECT a set of rows. How can I exclude the XML declaration from each row in the result set? My query currently looks like this, I'm executing it through Spring JDBC:
SELECT XMLSerialize(CONTENT t1.xmltext) FROM myschema.event t1 WHERE XMLEXISTS('$e/Event' PASSING XMLTEXT AS "e") ORDER BY t1.time DESC
After selecting, in my application I convert each row into a String and concatenate all rows into one big string in order to parse it into a DOM model. I get a parser exception (org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed) because there are multiple XML declarations in my big string. Of course, I could manually check the String of each row whether it starts with the XML declaration, but it would be nicer if I could instruct the DB not to add it in the first place. Is there a way?
Thanks!
-- DanielaHi,
A couple of options I can think of :
SELECT XMLSerialize(CONTENT
XMLtransform(t1.xmltext,
xmltype('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/"><xsl:copy-of select="*"/></xsl:template>
</xsl:stylesheet>')
FROM myschema.event t1
WHERE XMLEXISTS('$e/Event' PASSING XMLTEXT AS "e")
ORDER BY t1.time DESC
;or simply,
SELECT XMLSerialize(CONTENT
extract(t1.xmltext,'/')
FROM myschema.event t1
WHERE XMLEXISTS('$e/Event' PASSING XMLTEXT AS "e")
ORDER BY t1.time DESC
; -
How to show an image on each row of the report?
I have created a report in apex. i want to display image on each row in a column of the report.
How can i do this?See About BLOB Support in Forms and Reports in the documentation.
There's an OBE tutorial that followed the introduction of declarative BLOB support in 3.1 as well.
In future please
<li>Search the forum before posting a new question. Most questions (including this one) have been asked and answered before.
<li>Include as much relevant information with your question as possible, starting with:
- APEX version
- DB version and edition
- Web server architecture (EPG, OHS or APEX listener)
- Browser(s)/version(s) used
- Theme
- Templates
- Region type(s)
<li>Change your forum handle to something better than "845927". -
hello all,
here i have a basic problem ,
i am given a text file ie. note pad which has around 100 rows ,
each row represents each employee details,
now i have to convert each row into 1 idoc,
for this i had created one datatype whose structure coincides with the employee fields and after creating the message interface i mapped it with the all ready imported idoc,can any one tell me how to further with little details, any help will be appreciated ,its very urgent, thank you.Hi Kutumba,
In ID u need to create atlest one business service with a Sender File Communication Channel to receive the file and one Business System for the R/3 System with a Receiver IDoc Communication Channel to receive the Idoc XML.
U also need to create the Receiver and the Sender Agreements.
U can also go through this link:
https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/6bd6f69a-0701-0010-a88b-adbb6ee89b34
Try doing this and let me know if u face any further problems.
Regards
Neetu -
Multipe rows to Multiple Columns - Oracle9i
Hi
I know there have been similar threads however I haven't found a suitable solution from them.
I'm using Oracle 9i and trying to convert multiple rows to multiple columns.
CREATE TABLE TEST_VALUES
CUSTOMER_ID NUMBER(10),
VAL VARCHAR2(100)
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(10,'VAL1');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(10,'VAL2');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(20,'VAL1');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(30,'VAL3');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(30,'VAL4');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(40,'VAL5');
INSERT INTO TEST_VALUES(CUSTOMER_ID,VAL)VALUES(40,'VAL6');
COMMIT;
SELECT * FROM TEST_VALUES
CUSTOMER_ID VAL
10 VAL1
10 VAL2
20 VAL1
30 VAL3
30 VAL4
40 VAL5
40 VAL6
I want one row per CUSTOMER_ID with two columns to store the values
CUSTOMER_ID FIRST_VAL SECOND_VAL
10 VAL1 VAL2
20 VAL1 NULL
30 VAL3 VAL4
40 VAL5 VAL6
There are only 6 possible 'VAL' values however there are a large number of CUSTOMER_id values. I tried DECODE as suggested elsewhere but that creates 6 columns. However I only want two output columns as there are no more than two values per customer.
If I had 11g I would try pivot, can anyone please suggest any solutions?
Many Thanks
GBThanks Bhushan, that was nearly what I was after. For CUSTOMER 20 it gave
20 VAL1 VAL1
so to make it
20 VAL1 NULL
I just changed your query slightly to
select distinct
customer_id,
min(val) over (partition by (customer_id)) val1,
CASE WHEN max(val) over (partition by (customer_id)) = min(val) over (partition by (customer_id)) THEN NULL
ELSE max(val) over (partition by (customer_id)) END val2
from test_values
order by customer_id
and that gives the correct results. Just checking the full data set now.
Thanks for your help.
Maybe you are looking for
-
After upgrade 11.1.1.5 to 11.1.1.6, the Web Query (.iqy) does not work. After open the .iqy file in Excel, entered the user and password, it only pulls in "PK" in one cell, instead of the expected analysis report. This was working in 11.1.1.5 before
-
2 GB memory ample enough to handle iMovie tasks?
Hello. I'm about to get a 2.33 GHz iMac with 2 GB memory. If any of discussion-board users have 2 GB memory, I'm curious if any of you find your machines locking up or not up to the task of handling iMovie projects in any way. Yea, yea: I know: More
-
Can i have 2 apple ids on 1 iPad?
Is it possible to have 2 apple ids on 1 ipad?
-
How can I add duplicate contacts on my iPhone
I know that the majority of related Qs on here are about deleting duplicated contacts; and yes that can be a pain, but I want to go the other way. After I have spent ages filling in all the contact ino for someone [workwise etc], I often need to have
-
Question-Recovering tablespace in physical Standby...Oracle 10g?
Hello: I have an interesting issue. I thought I will ask the group. I have a physical standby database in 10g. Once of the datafile is in inconsistent state due to a crashed DB during recovery from the last time around. I do not have any archivelogs,