Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read an XML data in sql server?

I have an XML

<Table>
  <Columns>
  <Column Name='Name' Datatype='varchar(100)'/>
  </Columns>
  <Rows>
  <Row Name='Test' Number='123'/>
  </Rows>
</Table>

I want to read Name value in the provided xml in sql server How Can I do that?

I tried using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`Declare @XMl XML 
select @xml= cast(Data as xml) from Table
select y.value('Name[1]','varchar(100)') as Name
@xml.nodes(//Row) as x(y)` 

but am getting null values as result set How can I acheieve this in sql server

>Solution :

Like this:

    declare @doc xml = 
    '<Table>
      <Columns>
      <Column Name="Name" Datatype="varchar(100)"/>
      </Columns>
      <Rows>
      <Row Name="Test" Number="123"/>
      </Rows>
    </Table>'
    
    select r.value('@Name', 'varchar(200)') Name
    from @doc.nodes('/Table/Rows/Row') n(r)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading