Getting Started with Adobe After Effects - Part 6: Motion Blur
Ask
Ask questions, discuss or help others by answering
Related Posts · View All
SQL Server 141
TSQL 75
SSRS 70
SSIS 66
XML 54

Top Categories · View All
SQL Server 141
TSQL 75
SSRS 70
SSIS 66
XML 54

Need help in XML Data Extraction

May 25 2011 12:00AM by Praveen   

Hi Jacob I need ur help in extracting data from a typed xml (xml with namespace) in sql server. Help me in getting this done.

my sample xml is give below for ur reference:

<AuditTrail xmlns="http://AuditSchema"> 
  <Table TableName="tblClient"> 
    <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="ClientID"> 
      <Before>2919529</Before> 
      <After>2919529</After> 
    </Field> 
        <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="DateAltered"> 
      <Before>2008-02-02T13:37:32.483</Before> 
      <After>2008-02-02T13:37:32.983</After> 
    </Field> 
    <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="tempClientID"> 
      <Before xsi:nil="true" /> 
      <After xsi:nil="true" /> 
    </Field> 
  </Table> 
</AuditTrail>

From the above xml code i need to extract the value of "DateAltered" Field and above xml will be stored in a typed xml column of a DB table.

Submitted under: Microsoft SQL Server · XML ·  ·  · 


Praveen
1443 · 0% · 14

1 Replies

  • Try This

    DECLARE @x XML
    SELECT @x = '
    <AuditTrail xmlns="http://AuditSchema"> 
      <Table TableName="tblClient"> 
        <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="ClientID"> 
          <Before>2919529</Before> 
          <After>2919529</After> 
        </Field> 
            <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="DateAltered"> 
          <Before>2008-02-02T13:37:32.483</Before> 
          <After>2008-02-02T13:37:32.983</After> 
        </Field> 
        <Field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FieldName="tempClientID"> 
          <Before xsi:nil="true" /> 
          <After xsi:nil="true" /> 
        </Field> 
      </Table> 
    </AuditTrail>'
    
    ;WITH XMLNAMESPACES (
        default 'http://AuditSchema'
    )
    SELECT
        x.value('Before[1]','VARCHAR(30)') AS Before,
        x.value('After[1]','VARCHAR(30)') AS After
    FROM @x.nodes('/AuditTrail/Table/Field[@FieldName="DateAltered"]') a(x)
    
    /*
    Before                         After
    ------------------------------ ------------------------------
    2008-02-02T13:37:32.483        2008-02-02T13:37:32.983
    */
    
    commented on May 25 2011 10:42AM
    Jacob Sebastian
    1 · 100% · 32002

Your Reply


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]