Solved

Using xml data type in a query

Posted on 2009-07-15
7
198 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
  • 4
  • 2
7 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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 73

Expert Comment

by:sdstuber
ID: 24868647
what database?  The functions to manipulate xml vary by platform
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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 74

Expert Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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.​
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

11 Experts available now in Live!

Get 1:1 Help Now