Solved

Extract Value From XML Field in TSQL For SQL Server 10.50.4033.0

Posted on 2015-01-19
6
280 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL server Varchar and nvarchar, GMT_DATE 23 50
.htaccess file settings 4 36
sql server query? 6 28
BULK INSERT most recent CSV 19 21
In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now