Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you read XML Data in SQL Server 2005 [closed]

I have a table which has XMLData column as TEXT. How can i read the data from this column

like image 680
Learning Singh Avatar asked Mar 16 '26 21:03

Learning Singh


1 Answers

As long as that column is of type TEXT, you won't be able to do anything useful with it, really. TEXT also has been deprecated and will be removed in a future version of SQL Server - stop using it.

If it stores XML and only XML - make it of datatype XML.

One you have that, you can either extract individual items of information from that XML using XPath and XQuery - something like:

SELECT
    YourXMlColumn.value('(/Root/SomeItems/Item/FirstName)[1]', 'varchar(50)') as 'FirstName',
    YourXMlColumn.value('(/Root/SomeItems/Item/Age)[1]', 'int') as 'Age'
FROM
    dbo.YourTable
WHERE
    (some condition)

or if you have multiple items in a list-like structure inside your XML, you can create a "pseudo-table" of XML items based on an XPath expression.

So your plan of action should be:

  1. make this column use the appropriate datatype - XML
  2. tell us in more detail what kind of XML you have stored in there, and what you want to get from that XML
like image 78
marc_s Avatar answered Mar 19 '26 12:03

marc_s