?
Solved

Microsoft SQL XML Processing

Posted on 2016-08-19
3
Medium Priority
?
71 Views
Last Modified: 2016-09-24
I have an XML String that is stored in a SQL table as a XML field.  I need to be able to extract the following fields using TSQL.

Date
DeviceDate
UserName
FirstName
LastName
ResponseID
Job_Num value (in the Response nodes)

I have tried:
SELECT  
        x.XmlCol.value('(Response/Label)[3]','VARCHAR(100)') as xLable,
             x.XmlCol.value('(Response/Value)[3]','VARCHAR(100)') as xValue
FROM    gcREsponses b
            CROSS APPLY b.gcResponse_formXML.nodes('/CanvasResult/Submissions/Submission/Sections/Section/Screens/Screen/Responses') x(XmlCol)

Which works for that particular form, but I cannot always count on Job_Num being in the 3rd element.  It could change based on the form type being submitted.

Any help would be appreciated.



<CanvasResult>
  <TotalPages>1</TotalPages>
  <CurrentPage>1</CurrentPage>
  <Submissions>
    <Submission Id="25934640">
      <Form Id="1130317">
        <Name>Jobsite Visit Report</Name>
        <Status>published</Status>
        <Version>17</Version>
      </Form>
     <Date>2016.08.19 15:33:25</Date>
      <DeviceDate>2016.08.19 15:33:21</DeviceDate>
      <UserName>tfelker@anthonysylvan.com</UserName>
      <FirstName>Tyson</FirstName>
      <LastName>Felker</LastName>

      <ResponseID>302192D3-5B2C-4F9C-945D-FF299FF6EF81</ResponseID>
      <Sections>
        <Section>
          <Name>Jobsite Check-In</Name>
          <Screens>
            <Screen>
              <Name>Jobsite Check-In</Name>
              <Responses>
                <Response Guid="A0F4608DFD5B33225F09E03E5724BBBED79A54D4">
                  <Label>Customer Name</Label>
                  <Value>Linn</Value>
                  <Type>Text Box</Type>
                </Response>
                <Response Guid="644602B41B22A484C079CA37F5BCE9F718EBA576">
                  <Label>Project Manager</Label>
                  <Value>Tyson Felker</Value>
                  <Type>Text Box</Type>
                </Response>
                <Response Guid="A3610456E677A9D076CDE2FA6D5E8703D5BA3E0E">
                  <Label>Job_Num</Label>
                  <Value>825115</Value>
                  <Type>Text Box</Type>
                </Response>
                <Response Guid="B5FA40E6998CC41AE0253C21594C45A06E3BCC62">
                  <Label>Type of Visit</Label>
                  <Value>Jobsite Visit</Value>
                  <Type>Value List</Type>
                </Response>
                <Response Guid="670EA728025BD292E1E4C51219A273151401C6E0">
                  <Label>Notes From Visit</Label>
                  <Value>Discussed redesign</Value>
                  <Type>Multi-Line Text</Type>
                </Response>
                <Response Guid="C3B98287ECD03F2A44AAC636079C2FFBFA613243">
                  <Label>Picture 1</Label>
                  <Value>1733636793</Value>
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="EEB69B0F77A230FDF7667EC13966841E4DBA3C51">
                  <Label>Picture 2</Label>
                  <Value />
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="943560DFD0FFA5961863C872DD1B155054B8D5CE">
                  <Label>Picture 3</Label>
                  <Value />
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="387F6AB8EEA60A6F018C20FE5A516FB8A392944B">
                  <Label>Picture 4</Label>
                  <Value />
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="CA2B6DB96D593C541A057FC3FD5ECD8343DDC058">
                  <Label>Picture 5</Label>
                  <Value />
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="74CCDE93909A462397A98ECE9EE367121D8A2040">
                  <Label>Picture 6</Label>
                  <Value />
                  <Type>Image Capture</Type>
                </Response>
                <Response Guid="BB8D608210C4D9FD4A9F45959B556A759B6FA248">
                  <Label>GPS </Label>
                  <Value>Lat:30.278769,Lon:-97.846046,Acc:2277.691650,Alt:236.837006,Bear:-1.000000,Speed:-1.000000,Time:Fri, 19 Aug 2016 10:32:46 -0500</Value>
                  <Type>GPS</Type>
                </Response>
                <Response Guid="4C02BDC6B1E9090A741698F8CFB6525AF1CAADB2">
                  <Label>Date</Label>
                  <Value>08/19/2016</Value>
                  <Type>Date</Type>
                </Response>
                <Response Guid="B0FC3C7160BFDEB372CA5C516884FEA19AC903B4">
                  <Label>Cell_Num</Label>
                  <Value />
                  <Type>Text Box</Type>
                </Response>
              </Responses>
            </Screen>
          </Screens>
        </Section>
      </Sections>
    </Submission>
  </Submissions>
</CanvasResult>
0
Comment
Question by:sjterrell
[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
3 Comments
 
LVL 23

Assisted Solution

by:Snarf0001
Snarf0001 earned 1000 total points
ID: 41763642
If you know specifically that it's Job_Num that you have to query, then you'll have an easier time grabbing the value directly with a filter, instead of doing a cross apply against everything and then targeting that node.

Also, you can specify all named children with "//name" instead of needing to specify the entire xpath, just less typing.  Depends of course on whether or not you'll have other nodes named that in other levels.

Give this a shot:

select 
	r.gcResponse_formXML.value('(CanvasResult/Submissions/Submission/Date)[1]', 'datetime') as DeviceDate,
	r.gcResponse_formXML.value('(CanvasResult/Submissions/Submission/UserName)[1]', 'varchar(100)') as UserName,
	r.gcResponse_formXML.value('(CanvasResult/Submissions/Submission/FirstName)[1]', 'varchar(100)') as FirstName,
	r.gcResponse_formXML.value('(CanvasResult/Submissions/Submission/LastName)[1]', 'varchar(100)') as LastName,
	r.gcResponse_formXML.value('(//Responses/Response[Label=''Job_Num'']/Value)[1]', 'varchar(100)') as Job_Num
from gcREsponses r

Open in new window

0
 
LVL 35

Accepted Solution

by:
ste5an earned 1000 total points
ID: 41764222
Use a XPath filter like .nodes('/[..]/Response[Label="Job_Num"]') . E.g.

DECLARE @Xml XML =  N'
<CanvasResult>
   <TotalPages>1</TotalPages>
   <CurrentPage>1</CurrentPage>
   <Submissions>
     <Submission Id="25934640">
       <Form Id="1130317">
         <Name>Jobsite Visit Report</Name>
         <Status>published</Status>
         <Version>17</Version>
       </Form>
      <Date>2016.08.19 15:33:25</Date>
       <DeviceDate>2016.08.19 15:33:21</DeviceDate>
       <UserName>tfelker@anthonysylvan.com</UserName>
       <FirstName>Tyson</FirstName>
       <LastName>Felker</LastName>
       <ResponseID>302192D3-5B2C-4F9C-945D-FF299FF6EF81</ResponseID>
       <Sections>
         <Section>
           <Name>Jobsite Check-In</Name>
           <Screens>
             <Screen>
               <Name>Jobsite Check-In</Name>
               <Responses>
                 <Response Guid="644602B41B22A484C079CA37F5BCE9F718EBA576">
                   <Label>Project Manager</Label>
                   <Value>Tyson Felker</Value>
                   <Type>Text Box</Type>
                 </Response>
                 <Response Guid="A3610456E677A9D076CDE2FA6D5E8703D5BA3E0E">
                   <Label>Job_Num</Label>
                   <Value>825115</Value>
                   <Type>Text Box</Type>
                 </Response>
               </Responses>
             </Screen>
           </Screens>
         </Section>
       </Sections>
     </Submission>
   </Submissions>
 </CanvasResult>
 ';


 SELECT	Submission.value('Date[1]', 'DATETIME') AS SubmissionDate,
	Submission.value('DeviceDate[1]', 'DATETIME') AS SubmissionDeviceDate,
	Submission.value('UserName[1]', 'NVARCHAR(255)') AS SubmissionUserName,
	Submission.value('FirstName[1]', 'NVARCHAR(255)') AS SubmissionFirstName,
	Submission.value('LastName[1]', 'NVARCHAR(255)') AS SubmissionLastName,
	ResponseJobNum.value('Value[1]', 'INT') AS ResponseJobNumValue
 FROM    @Xml.nodes('/CanvasResult/Submissions/Submission') A ( Submission )
	OUTER APPLY Submission.nodes('Sections/Section/Screens/Screen/Responses/Response[Label="Job_Num"]') B ( ResponseJobNum );

Open in new window


Don't use '//' in production code, when you need it often. Cause it is much slower than a full XPath addressing the correct element. Also //tagName can retrieve elements from different branches and levels.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

762 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