?
Solved

Using xml data type in a query

Posted on 2009-07-15
7
Medium Priority
?
203 Views
Last Modified: 2012-05-07
I have a field in a table which is xml.

The xml embedded in the field ClientDetails as follows

Transaction>
    <Clients>
    <ClientDetails>
      <Title>Mrs.</Title>
      <FirstName>Jackie</FirstName>
      <MiddleNames />
      <Surname>Trial</Surname>
      <DateOfBirth />
    </ClientDetails>
    <ClientDetails>
      <Title>Mr.</Title>
      <FirstName>Ian</FirstName>
      <MiddleNames />
      <Surname>Test</Surname>
      <DateOfBirth />
    </ClientDetails>
    <ContactDetails>
      <AddressDetails>
        <AddressLine1>The Hollows</AddressLine1>
        <AddressLine2>2 Smith Lane</AddressLine2>
        <AddressLine3>Doncaster</AddressLine3>
        <AddressLine4>South Yorkshire</AddressLine4>
        <AddressLine5 />
        <Postcode>D3 5TY</Postcode>
        <Country />
      </AddressDetails>
      <EmailAddress>mark.smith@irwinmitchell.com</EmailAddress>
      <PhoneNumber />
      <MobileNumber />
    </ContactDetails>
  </Clients>
</Transaction>

I am looking to extract some of the information in the xml to use in a select criteria, but I am not sure how to do it.

i.e. Select Firstname, Surname
from table

Can anybody help?

Thanks
0
Comment
Question by:halifaxman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24865062
There are a couple of xml functions you can use to extract data. I am attaching a screen shot of the functions as well as what they returned using your data.

If for some reason the image is not good, I can post the code. I just wanted to demonstrate the code and results together.
untitled.JPG
0
 

Author Comment

by:halifaxman
ID: 24867474
Thanks for the answer

I have used the following

SELECT xmldata.query('(/Transaction/Clients/ClientDetails[1]/Firstname/text())') as FN
from xmltest

I get the error message

Cannot find either column "xmldata" or the user-defined function or aggregate "xmldata.query", or the name is ambiguous
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24868647
what database?  The functions to manipulate xml vary by platform
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 

Author Comment

by:halifaxman
ID: 24869223
sorry its a sql 2008 database
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24869229
I guess I jumped the gun :)  I get so used to working in MS stuff I sometimes forget to look at the zone!! Can you answer sdstuber's question in order that the answer may be revised.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24869234
Just to confirm:  Sql Server, correct?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24869242
"xmldata" was the name of the column in my particular example. You should change that to whatever the name of your XML column is.
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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