How to get the ROOT node name from SQL Server

KS Kian Seng picture KS Kian Seng · Sep 6, 2013 · Viewed 12.5k times · Source

I have a table where ID is integer and XML is XML data type.

ID   XML
----------------------
1    <Form1>...</Form1>
2    <Form1>...</Form1>
3    <Form2>...</Form2>
4    <Form3>...</Form3>

How do I get the result below?

ID   XML
-------------
1    Form1
2    Form1
3    Form2
4    Form3

Answer

podiluska picture podiluska · Sep 6, 2013

Use the local-name() function

 select ID, XML.value('local-name(/*[1])','varchar(100)')
 from yourtable