I'm trying to retrieve the value of a xml node that is stored as an xml datatype in a sql server column. For example my xml column data is:
<fields>
<field id="StudentName">John Smith</field>
<field id="StudentID">1310021003</field>
<field id="SchoolName">Little Kid Elementary School</field>
</fields>
I want to retrieve the StudentID. When I run the script below I get null.
select MyColumnName.value('(/fields/field/@StudentID)[1]', 'varchar(20)') as StudentId from MyTable
[bonus question] - I would also like to query the table by studentid if possible, for example: select * from MyTable where MyColumnName.value('(/fields/field/@StudentID)[1]', 'varchar(20)') = '1310021003'