Solved

Microsoft SQL XML Processing

Posted on 2016-08-19
3
44 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
3 Comments
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 250 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 33

Accepted Solution

by:
ste5an earned 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

786 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