Link to home
Start Free TrialLog in
Avatar of sjterrell
sjterrellFlag for United States of America

asked on

Microsoft SQL XML Processing

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>
SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial