SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extracting xml data from text column


Extracting xml data from text column

Author
Message
ott2497
ott2497
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 61
I'm very new sql server (oracle dba). I was assigned to work with sql server 2005. The user wants a report where data is in xml format. The table creation's script is below. The xml column, datatype text, has xml format data. I need to extract it and create report. If I could extract into a table, then I'm all set. But I don't know how to extract it.
Any help will be appreciated.

Thanks

CREATE TABLE [dbo].[job](
[id] [int] NOT NULL,
[submitTime] [datetime] NULL,
[stopTime] [datetime] NULL,
[priority] [int] NULL,
[xml] [text] NULL, <=== xml data in this column
[display] [bit] NULL,
[title] [varchar](256) NULL,
[author] [varchar](50) NULL,
[taskstarts] [int] NULL,
[taskstops] [int] NULL,
[ntasks] [int] NULL,
[failed] [bit] NULL,
[userdata] [text] NULL,
[reservedtime] [int] NULL,
[nodename] [varchar](256) NULL,
[actualSubmitTime] [datetime] NULL,
[idMain] [int] NULL,
CONSTRAINT [PK_job_jobid] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY
davidsalazar01
davidsalazar01
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 374
Hi,

Did you ever get an answer/solution on this topic? I have the same issue and don't know the answer.

Please let me know.

Thanks!

-Dave



Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26309 Visits: 17553
davidsalazar01 (2/12/2013)
Hi,

Did you ever get an answer/solution on this topic? I have the same issue and don't know the answer.

Please let me know.

Thanks!

-Dave


This thread is 2 1/2 years old. You will likely have better luck starting a new one.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
ott2497
ott2497
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 61
Since I got no response and I found that I could do it in Oracle, so I extracted data daily from sqlserver database and loaded into Oracle database. From then, I wrote pl/sql using extractvalue to get data that I wanted. It's more complicated but I had reports for users.

I found that dealing with xml in Oracle is easier than sqlserver.

Good Luck!
davidsalazar01
davidsalazar01
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 374
Ott,

Thanks for the update it's appreciated.

-Dave



Davio
Davio
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 1204
I was asked to do the very same thing with XML a few weeks ago and this is a slightly simplified version of what I came up with:

declare @stringXML varchar(max), @XMLData as xml
set @stringXML =
'<app-data>
<FlowsNotification trandate="09/19/2008" type="Contribution" manager="BCA" custodian="">
<Contribution>
<transaction id="cm2428382">
<snam>TF1012</snam>
<style>DFISPM</style>
<date>09/18/2008</date>
<include>true</include>
<amount>200000.00</amount>
<originalamount>200000.00</originalamount>
<description>$JNL :Funds trans from acct (97302287)</description>
<accountid>97527748</accountid>
<custid>164</custid>
<custodian>CS</custodian>
<isAccountInCDB>true</isAccountInCDB>
</transaction>
</Contribution>
</FlowsNotification>
<account-data>
<account custAcctNo="97527748" custID="164" />
</account-data>
</app-data>'

set @XMLData = CAST(@stringXML AS XML)
select @XMLData as XMLData into #XMLTable

select x.y.value('Contribution[1]/transaction[1]/@id[1]','VARCHAR(20)') AS tranid,
x.y.value('@trandate[1]','Date') AS NotificationTranDate,
x.y.value('@manager[1]', 'CHAR(10)') AS MgrCode,
x.y.value('Contribution[1]/transaction[1]/snam[1]','VARCHAR(50)') AS ACCTsnam,
x.y.value('Contribution[1]/transaction[1]/style[1]','VARCHAR(20)') AS style,
x.y.value('Contribution[1]/transaction[1]/date[1]','Date') AS TranDate,
Case when x.y.value('Contribution[1]/transaction[1]/include[1]','varchar(50)') = 'true' then 1 else 0 end AS include,
x.y.value('Contribution[1]/transaction[1]/amount[1]','Float') AS amount,
x.y.value('Contribution[1]/transaction[1]/originalamount[1]','Float') AS originalamount,
x.y.value('Contribution[1]/transaction[1]/description[1]','VARCHAR(255)') AS tranDescription,
x.y.value('Contribution[1]/transaction[1]/accountid[1]','VARCHAR(50)') AS accountid,
x.y.value('Contribution[1]/transaction[1]/custid[1]','integer') AS custid,
x.y.value('Contribution[1]/transaction[1]/custodian[1]','varchar(20)') AS custodian,
Case when x.y.value('Contribution[1]/transaction[1]/isAccountInCDB[1]','varchar(50)') = 'true' then 1 else 0 end AS isAccountInCDB
FROM #XMLTable T
CROSS APPLY T.XMLData.nodes('app-data/FlowsNotification') x(y)

drop table #XMLTable

The query pulls out both elements and values of the sample XML provided.

I found the XML examples by Jacob Sebastian site very helpful http://beyondrelational.com/modules/2/blogs/28/posts/10279/xquery-labs-a-collection-of-xquery-sample-scripts.aspx.



davidsalazar01
davidsalazar01
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 374
I ended up using ths logic below and it did the trick.

convert(xml,pm.XmlProfile_TXT).value('/XMLProfile[1]/DID[1]', 'varchar(30)')

Thanks everybody for your input and good suggestions! This post is now completed.

-Dave



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search