Solved

Extract Value From XML Field in TSQL For SQL Server 10.50.4033.0

Posted on 2015-01-19
6
320 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

860 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