adsense

Friday, November 20, 2020

SQL get specific element Count in XML

 Consider this XML


Suppose you want to get count of student ids. You can use SQL to get the count as follows.

declare @xml XML;

set @xml ='<Students>

    <Student>

        <Id>001</Id>

        <Fname>Leesa</Fname>

        <Sname>Humpry</Sname>

    </Student>

    <Student>

        <Id>002</Id>

        <Fname>Clara</FName>

        <Sname>Margrette</Sname>

    </Student>

  </Students>';

select @xml.value('count(/Students/Student/Id)', 'INT') AS 'Count'

Cheers

Samitha

No comments:

Post a Comment