I have a column in a database I'm working on that contains XML, but is stored as varchar
. I can CAST()
the column to XML, but it's not clear how I would fetch anything from this column using XPATH or otherwise.
EXTRACT()
seems specific to MYSQL only, and .value()
doesn't work in any form I've tried it. I'm hoping I'm just doing it wrong here.
In case there's a version-specific solution, querying @@VERSION
gets me the following:
Microsoft SQL Server 2012 (SP4-GDR) (KB4583465) - 11.0.7507.2 (X64) Nov 1 2020 00:48:37 Copyright (c) Microsoft Corporation Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor)
A simplified example of the XML I'm working with is below. Any examples on how to extract the values for any given element would be greatly appreciated.
<?xml version="1.0" encoding="UTF-8"?>
<ExampleXML>
<Version></Version>
<NameSpace></NameSpace>
<ClientID></ClientID>
<ClientName></ClientName>
<ShipToInfo>
<Name></Name>
<Address1></Address1>
<Address2 />
<City></City>
<State></State>
<Postal></Postal>
<Country></Country>
<Phone></Phone>
<Fax></Fax>
<EIdType />
<EId></EId>
</ShipToInfo>
<BillToInfo>
<Name></Name>
<Address1></Address1>
<Address2 />
<City></City>
<State></State>
<Postal></Postal>
<Country></Country>
<Phone></Phone>
<Fax></Fax>
<EIdType />
<EId></EId>
</BillToInfo>
</ExampleXML>
Edit: Example of .value()
SELECT CAST(CONTENT AS xml).value('(/ExampleXML/ShipToInfo/@EId)[1]', 'int')
The resulting column contains NULL
Edit 2: It looks like the final version that worked for me is as follows:
SELECT top 1 ET.*, t2.c.value('EId[1]', 'varchar(max)') AS store_num
FROM example_table ET
OUTER APPLY (SELECT CAST(ET.CONTENT AS xml) AS realxml) t1
OUTER APPLY t1.realxml.nodes('//ExampleXML/ShipToInfo') AS t2(c);