In a table in my data base I have a column in which the entire entry is a long XML String with the following structure:
<Group1>
<Title>
<Name>John Doe</Name>
<Phone>555-3421</Phone>
<Email>[email protected]</Email>
(+more)
</Title>
</Group1>
This is my SELECT Statement and here is what it outputs:
SELECT TheKey, TheData FROM MyTable;
Output in SQL Server Management Studio:
TheKey TheData
10000 <Group1><Title><Name>John Doe</Name><Phone>893-3421</Phone><Email>[email protected]</Email></Title></Group1>
10001 <Group1><Title><Name>Mary Sue</Name><Phone>381-2342</Phone><Email>[email protected]</Email></Title></Group1>
10002 <Group1><Title><Name>Mark Dark</Name><Phone>312-7626</Phone><Email>[email protected]</Email></Title></Group1>
10003 <Group1><Title><Name>Garth Dan</Name><Phone>341-4572</Phone><Email>[email protected]</Email><State>California</State></Title></Group1>
I would like to write some sort of MS SQL query that will return the data like this to me:
TheKey Name Phone Email State
10000 John Doe 893-3421 [email protected] NULL
10001 Mary Sue 381-2342 [email protected] NULL
10002 Mark Dark 312-7626 [email protected] NULL
10003 Garth Dan 341-4572 [email protected] California
Notice how the last entry had an extra XML tag <State>
which the other entries did not. I would like it to be flexible like this - using some sort of parent/child references?
Any help doing this would be greatly appreciated, I just can't seem to find anything like it anywhere =)
The simplest way to convert data from SQL tables into XML format is to use the FOR XML AUTO and FOR XML PATH clauses. The FOR XML AUTO clause converts each column in the SQL table into an attribute in the corresponding XML document. Execute the following script:
FROM OPENXML query to get the XML data into a table on a DB. Once you have the data in a table on a DB, the response rate to subsequent queries is drastically improved over querying the XML data source directly.
The XMLData column is an XML data type, it will output a hyperlink as shown below: Clicking on the hyperlink, in the above image, will open another tab within SSMS with the XML data displayed as shown below.
Oracle 10g Release 2 introduced the XMLTABLE operator, which allows you to project columns on to XML data in an XMLTYPE, making it possible to query the data directly from SQL as if it were relational data. This article presents some simple examples of its use.
If your XML column is stored as type XML, then you can use an XPath query to get each column. Like this:
SELECT TheKey,
TheData.value('(/Group1/Title/Name)[1]', 'varchar(100)') AS Name,
TheData.value('(/Group1/Title/Phone)[1]', 'varchar(100)') AS Phone,
TheData.value('(/Group1/Title/Email)[1]', 'varchar(250)') AS Email,
TheData.value('(/Group1/Title/State)[1]', 'varchar(100)') AS [State]
FROM MyTable
If the column MyData
is varchar instead of XML, then you can cast it during the query. Like this:
SELECT TheKey,
Cast(TheData AS XML).value('(/Group1/Title/Name)[1]', 'varchar(100)') AS Name,
Cast(TheData AS XML).value('(/Group1/Title/Phone)[1]', 'varchar(100)') AS Phone,
Cast(TheData AS XML).value('(/Group1/Title/Email)[1]', 'varchar(250)') AS Email,
Cast(TheData AS XML).value('(/Group1/Title/State)[1]', 'varchar(100)') AS [State]
FROM MyTable
Btw, I got this information from this other SO article. (vote it up to make it easier for others to find it) How to query xml column in tsql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With