I'm getting data from a table where one of the columns is in XML format. The column is called Updated and the table is Audit. The fields look like this:
<Fields><Field Name="DateFrom"/><Field Name = "Type 1"/><Field Name = "Type 2/></Fields>
<Fields><Field Name = "DateFrom"/></Fields>
<Fields><Field Name="DateFrom"/><Field Name = "Note"/><Field Name = "Type 1"/></Fields>
The XML field is part of a bigger query:
Select id, Updated
from Audit
The end will look something like the following, with ID being a non-XML column.
ID Updated
123 DateFrom, Type1, Type2
323 DateFrom
455 DateFrom, Note, Type1
I've tried some things I found on-line, but I'm not doing this correctly. One method I tried was:
Select Updated.value('/Fields/Field Name)[1]', 'nvarchar(max)') as NewUpdated from Audit.
Any ideas?
(Fields/Field/@Name)[1]. A cross apply could work like this:SELECT NewUpdated = A.B.value('@Name', 'nvarchar(max)') FROM Audit CROSS APPLY Audit.Updated.nodes('Fields/Field') AS A(B);.value(...)), there's no reason I can think of for why that wouldn't work unless you're using, say, SQL Server 2005.