Skip to content
Advertisement

How to return multiple values from XML element in SQL?

I need to pull information from the “Name” element from an XML column in SQL. An example of the XML is below:

I have this SQL Query which works for returning five of the “Name” elements:

That would be sufficient if every record had only five “Name” elements in the XML, but the number of “Name” elements varies from record to record.

How could I change my query to handle the variation from record to record?

Advertisement

Answer

Please try the following solution.

As @Larnu pointed out, it is much better to shred the XML as rows.

If needed it is very easy to filter out names with NULL values.

SQL

Output

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement