Solved

Extract Value From XML Field in TSQL For SQL Server 10.50.4033.0

Posted on 2015-01-19
6
341 Views
Last Modified: 2015-01-19
I can't find an example that I can copy from that works (or I can translate) for extracting a particular value from an XML field on a table within a SELECT statement.

I can do what I need using the example in the attached file where I explicitly declare an XML string with an example of the type of XML I am parsing but I cannot translate that to a SELECT statement that retrieves the value I need on each row of the many thousands of rows in the SQL table I'm looking at.

The attached file as I've said, shows an example of the structure of the XML inside a field on my table and I need to extract the ParticipantFunction and EndorsementParticipantFunction values from the XPath "Jv-Ins-Reinsurance/Placing/ContractSection/ContractMarket".

Can anyone help?
XML-Query-Example.sql
0
Comment
Question by:raynera
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40557418
Try this, fill in your table name instead of #tmpdb:
--http://www.ACORD.org/Standards/AcordMsgSvc/1
;WITH XMLNAMESPACES ('http://www.ACORD.org/standards/Jv-Ins-Reinsurance/2009-1'  AS WS)

SELECT
  Nodes.node.value('WS:ParticipantFunction[1]', 'varchar(10)')  ParticipantFunction,
  Nodes.node.value('WS:EndorsementParticipantFunction[1]', 'varchar(10)')  EndorsementParticipantFunction
FROM
#tmpdb cross apply myxml.nodes('WS:Jv-Ins-Reinsurance/WS:Placing/WS:ContractSection/WS:ContractMarket') AS Nodes(node);

Open in new window

0
 

Author Comment

by:raynera
ID: 40557442
Tried it...but got an error...

Msg 9506, Level 16, State 1, Line 3
The XMLDT method 'nodes' can only be invoked on columns of type xml.

CommunicationItem.citCommItem is my schema.table name and citBodyXML is the field.

I made a mistake and just noticed it after getting the error above...the citBodyXML field isn't an XML field type after all - DOH!

Ran the following...

--http://www.ACORD.org/Standards/AcordMsgSvc/1
;WITH XMLNAMESPACES ('http://www.ACORD.org/standards/Jv-Ins-Reinsurance/2009-1'  AS WS)

SELECT
  Nodes.node.value('WS:ParticipantFunction[1]', 'varchar(10)')  ParticipantFunction,
  Nodes.node.value('WS:EndorsementParticipantFunction[1]', 'varchar(10)')  EndorsementParticipantFunction
FROM
CommunicationItem.citCommItem cross apply citBodyXML.nodes('WS:Jv-Ins-Reinsurance/WS:Placing/WS:ContractSection/WS:ContractMarket') AS Nodes(node);
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40557447
then you can try something like convert(xml, citBodyXML)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40557453
Hm, sorry just tried it and cast/convert don't seem to work. Maybe use a temp table? I'll try with a varchar column myself as well.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40557454
Well, this works, using a CTE (common table expression) to get the xml field:
--http://www.ACORD.org/Standards/AcordMsgSvc/1
;WITH XMLNAMESPACES ('http://www.ACORD.org/standards/Jv-Ins-Reinsurance/2009-1'  AS WS)
,tmp as (select CAST(myxml as xml) as myxml from #tmpdb)
SELECT
  Nodes.node.value('WS:ParticipantFunction[1]', 'varchar(10)')  ParticipantFunction,
  Nodes.node.value('WS:EndorsementParticipantFunction[1]', 'varchar(10)')  EndorsementParticipantFunction
FROM
tmp cross apply myxml.nodes('WS:Jv-Ins-Reinsurance/WS:Placing/WS:ContractSection/WS:ContractMarket') AS Nodes(node);

Open in new window

0
 

Author Comment

by:raynera
ID: 40557477
Thanks very much - figured it out from that :)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question