15

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
KS Kian Seng
  • 319
  • 1
  • 6
  • 14

2 Answers2

32

Use the local-name() function

 select ID, XML.value('local-name(/*[1])','varchar(100)')
 from yourtable
podiluska
  • 50,950
  • 7
  • 98
  • 104
2

Try this

DECLARE @xml as xml
SET @xml = '<Form1>...</Form1>'
SELECT Nodes.Name.query('local-name(.)') FROM @xml.nodes('//*') As Nodes(Name)
Gayathri L
  • 1,407
  • 11
  • 13
  • We were using this method, but we ran into issues with "local-name(.)" being a slow operation. replacing the . with /*[1] as indicated in the above answer gave us better performance. – bmg002 Nov 28 '16 at 17:58