How to replace values in xml from sql? - sql

I have an xml in my SQL Server table like this:
<Category>
<Attributes>
<Attribute>
<Name>GeneratorOnBoard1</Name>
<Value>Yes</Value>
</Attribute>
<Attribute>
<Name>GeneratorOnBoard2</Name>
<Value>Yes</Value>
</Attribute>
</Attributes>
</Category>
I want to replace the value of GeneratorOnBoard1 from 'yes' to 'yes please'
but should not change the value of GeneratorOnBoard2.
If I use this:
declare #xml xml=''
select cast (replace (cast(#xml as nvarchar(max)), 'yes','yes please') as xml)
it might replace all the yes values.
What should I do?

Bit easy and alternative way is to use STUFF with PATINDEX as below
SELECT
STUFF(CAST(#xml AS NVARCHAR(MAX)),
PATINDEX('%Yes%', CAST(#xml AS NVARCHAR(MAX))), 3, 'Yes Please');

Have a look at using replace value of (XML DML).
declare #xml xml = '<Category>
<Attributes>
<Attribute>
<Name>GeneratorOnBoard1</Name>
<Value>Yes</Value>
</Attribute>
<Attribute>
<Name>GeneratorOnBoard2</Name>
<Value>Yes</Value>
</Attribute>
</Attributes>
</Category>';
set #xml.modify('replace value of (/Category
/Attributes
/Attribute[(Name/text())[1] = "GeneratorOnBoard1" and
(Value/text())[1] = "Yes"]
/Value/text())[1]
with "yes please"');

Related

Sum multiple nodes value on based on specific condition in XML in SQL Server 2014

Below is XML and I want to sum of "ClaimPayment.Amount" value where "ClaimPayment.TypeCode" is Indemnity. Means output would 10000. Remember nodes can be more.
<ZObject>
<Attribute Name="ClaimPayment">
<ZObject>
<Attribute Name="Re4eba255bf394bdbaccba77b6edca4f5">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Expense</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">3000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
<Attribute Name="R0ffc51fa96594b7989a7dec13a4ddd15">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Indemnity</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">50000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
<Attribute Name="R64b104f17aa94ffebb75ad0b2d8b2775">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Indemnity</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">50000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
</ZObject>
I have googled it and come upto this far:
create table #claims(id int identity(1,1) , customdata xml)
;WITH XMLNAMESPACES (
Default 'http://www.oceanwide.com/ZObject/2014',
'http://www.w3.org/2001/XMLSchema' as xs,
'http://www.w3.org/2001/XMLSchema-instance' as xsi)
insert into #claims(customdata )
Select CAST(CustomData AS XML)
from Claims.Resources_Claim cB
;WITH XMLNAMESPACES (
Default 'http://www.oceanwide.com/ZObject/2014',
'http://www.w3.org/2001/XMLSchema' as xs,
'http://www.w3.org/2001/XMLSchema-instance' as xsi)
select x.y.query('(//ZObject/Attribute[#Name="ClaimPayment.TypeCode"]/ZObject/Value/text())')
, ISNULL(CAST(CAST(CustomData AS XML).query('sum(/ZObject/Attribute/ZObject/Attribute/ZObject/Attribute[#Name="ClaimPayment.Amount"]/ZObject/Value/text())') as nvarchar(max)),'') AS 'amount'
,cb.*from #claims cB
CROSS APPLY CB.CustomData.nodes('/ZObject/Attribute[#Name = "ClaimPayment"]') as x(y)
where CB.customdata.exist('(//ZObject/Attribute[#Name="ClaimPayment.TypeCode"]/ZObject/Value[.="Indemnity"])')=1
You don't need to shred the XML with .nodes in this case, as you can sum it purely in XQuery
WITH XMLNAMESPACES
(
DEFAULT 'http://www.oceanwide.com/ZObject/2014',
'http://www.w3.org/2001/XMLSchema' as xs,
'http://www.w3.org/2001/XMLSchema-instance' as xsi
)
SELECT ID
, Amount = customdata.value('sum(
ZObject/Attribute/ZObject/Attribute/ZObject
[
Attribute[#Name = "ClaimPayment.TypeCode"]
/ZObject/Value[text() = "Indemnity"]
]/Attribute[#Name = "ClaimPayment.Amount"]
/ZObject/Value/text()
)', 'decimal(18,9)')
FROM #tbl AS t;
ID
Amount
1
100000.000000000
db<>fiddle
Please try the following solution.
A minimal reproducible example is not provided. So, I am shooting from the hip.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, customdata XML);
INSERT INTO #tbl (customdata) VALUES
(N'<ZObject xmlns="http://www.oceanwide.com/ZObject/2014" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Attribute Name="ClaimPayment">
<ZObject>
<Attribute Name="Re4eba255bf394bdbaccba77b6edca4f5">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Expense</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">3000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
<Attribute Name="R0ffc51fa96594b7989a7dec13a4ddd15">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Indemnity</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">50000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
<Attribute Name="R64b104f17aa94ffebb75ad0b2d8b2775">
<ZObject>
<Attribute Name="ClaimPayment.TypeCode">
<ZObject>
<Value xsi:typeName="xs:string">Indemnity</Value>
</ZObject>
</Attribute>
<Attribute Name="ClaimPayment.Amount">
<ZObject>
<Value xsi:typeName="xs:decimal">50000.0000</Value>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
</ZObject>
</Attribute>
</ZObject>');
-- DDL and sample data population, end
;WITH XMLNAMESPACES
(
DEFAULT 'http://www.oceanwide.com/ZObject/2014',
'http://www.w3.org/2001/XMLSchema' as xs,
'http://www.w3.org/2001/XMLSchema-instance' as xsi
)
SELECT ID
, Amount = SUM(c.value('(./text())[1]', 'DECIMAL(15,4)'))
FROM #tbl AS t
OUTER APPLY customdata.nodes('/ZObject/Attribute/ZObject/Attribute/ZObject[Attribute/ZObject/Value/text()="Indemnity"]/Attribute[#Name="ClaimPayment.Amount"]/ZObject/Value') AS t1(c)
GROUP BY ID;
Output
+----+-------------+
| ID | Amount |
+----+-------------+
| 1 | 100000.0000 |
+----+-------------+

Snowflake DB: XML parsing

I am trying to parse xml data in the table into key-value pair tables. The data consist of individual product xml as a row in a snowflake table. Each product has items under it. Each of the items has item attributes. The item attributes are varying for each items. So it need to be parsed as key-value rows. Here is sample data:
#insert product 1 to a temp table
insert into CLAIM3
select parse_xml($$<Product>
<ProductNumber>P_S001</ProductNumber>
<ProductName>Writing pad</ProductName>
<ItemsOfProduct>
<Item>
<ItemNumber>P1_01</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item1</AttributeID>
<AttributeValue>3-4</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item1</AttributeID>
<AttributeValue>Flamingo</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item1</AttributeID>
<AttributeValue>Blue</AttributeValue>
</Attribute>
</Attributes>
</Item>
<Item>
<ItemNumber>P1_02</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item2</AttributeID>
<AttributeValue>4-5</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item2</AttributeID>
<AttributeValue>Yellow</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item2</AttributeID>
<AttributeValue>Red</AttributeValue>
</Attribute>
</Attributes>
</Item>
</ItemsOfProduct>
</Product>
$$);
# insert product 2 to temp table
insert into CLAIM3
select parse_xml($$<Product>
<ProductNumber>P_S002</ProductNumber>
<ProductName>Writing Pen</ProductName>
<ItemsOfProduct>
<Item>
<ItemNumber>P2_01</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p2_item1</AttributeID>
<AttributeValue>3-4</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p2_item1</AttributeID>
<AttributeValue>Flamingo</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p2_item1</AttributeID>
<AttributeValue>Blue</AttributeValue>
</Attribute>
</Attributes>
</Item>
<Item>
<ItemNumber>P2_02</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p2_item2</AttributeID>
<AttributeValue>4-5</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p2_item2</AttributeID>
<AttributeValue>Yellow</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p2_item2</AttributeID>
<AttributeValue>Red</AttributeValue>
</Attribute>
</Attributes>
</Item>
</ItemsOfProduct>
</Product>
$$);
# view contents of table - shows two rows - each containing the district products inserted above
SELECT * FROM CLAIM3;
Here is how I am parsing the data. Took reference of https://medium.com/snowflake/working-with-xml-in-snowflake-part-i-8b4eca1c01fa
SELECT
XMLGET(src_xml, 'ProductNumber'):"$"::string AS SRC_PRODUCT_NUMBER,
XMLGET(XMLGET(XMLGET(src_xml, 'ItemsOfProduct'), 'Item'), 'ItemNumber') :"$"::string AS ITEM_NUMBER,
XMLGET(attributes.value, 'AttributeID'):"$" AS ATTRIBUTE_NAME,
XMLGET(attributes.value, 'AttributeValue'):"$" AS ATTRIBUTE_VALUE
FROM CLAIM3,
LATERAL FLATTEN(XMLGET(XMLGET(XMLGET(src_xml, 'ItemsOfProduct'), 'Item'), 'Attributes'):"$") attributes
The output is providing the attributes of item 1(P1_01, P2_01) of product 1 and product 2. It is skipping item 2(P1_02, P2_02) of product 1 and product 2. Can anyone help me understand what might be going wrong here?
SELECT XMLGET(src_xml,'ProductNumber'):"$":: STRING PRODUCT_NUMBER
,XMLGET(ITEMS.VALUE,'ItemNumber'):"$" :: STRING ITEM_NUMBER
,XMLGET(ATTRIBUTE.VALUE,'AttributeID'):"$" :: STRING ATTRIBUTE_ID
,XMLGET(ATTRIBUTE.VALUE,'AttributeValue'):"$" :: STRING ATTRIBUTE_VALUE
FROM CLAIM3 , LATERAL FLATTEN(src_xml:"$") ITEMSOFPRODUCT, LATERAL FLATTEN(ITEMSOFPRODUCT.VALUE:"$") ITEMS
, LATERAL FLATTEN(ITEMS.VALUE:"$") ATTRIBUTES
, LATERAL FLATTEN(ATTRIBUTES.VALUE:"$") ATTRIBUTE
--WHERE GET(ITEMS.VALUE,'#')='Item' ;
Does this work ?
The guts - I think you need to join to each level in your XML heirarchy ... e.g. two lots of lateral joins.
Let me know if this helps/solves :-)
Code can be copy/pasted and run straight into Snowflake :-)
with cte as (
select
parse_xml(
$$<Product>
<ProductNumber>P_S001</ProductNumber>
<ProductName>Writing pad</ProductName>
<ItemsOfProduct>
<Item>
<ItemNumber>P1_01</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item1</AttributeID>
<AttributeValue>3-4</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item1</AttributeID>
<AttributeValue>Flamingo</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item1</AttributeID>
<AttributeValue>Blue</AttributeValue>
</Attribute>
</Attributes>
</Item>
<Item>
<ItemNumber>P1_02</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item2</AttributeID>
<AttributeValue>4-5</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item2</AttributeID>
<AttributeValue>Yellow</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item2</AttributeID>
<AttributeValue>Red</AttributeValue>
</Attribute>
</Attributes>
</Item>
</ItemsOfProduct>
</Product>
$$
) src_xml
union
select
parse_xml(
$$<Product>
<ProductNumber>P2_S001</ProductNumber>
<ProductName>Writing pad</ProductName>
<ItemsOfProduct>
<Item>
<ItemNumber>P21_01</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item1</AttributeID>
<AttributeValue>3-4</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item1</AttributeID>
<AttributeValue>Flamingo</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item1</AttributeID>
<AttributeValue>Blue</AttributeValue>
</Attribute>
</Attributes>
</Item>
<Item>
<ItemNumber>P21_02</ItemNumber>
<Attributes>
<Attribute>
<Type>Defining</Type>
<AttributeID>Size_p1_item2</AttributeID>
<AttributeValue>4-5</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>SecondaryColour_p1_item2</AttributeID>
<AttributeValue>Yellow</AttributeValue>
</Attribute>
<Attribute>
<Type>Defining</Type>
<AttributeID>Colour_p1_item2</AttributeID>
<AttributeValue>Red</AttributeValue>
</Attribute>
</Attributes>
</Item>
</ItemsOfProduct>
</Product>
$$
)
)
SELECT
XMLGET(src_xml, 'ProductNumber') :"$" :: string AS SRC_PRODUCT_NUMBER,
XMLGET(attributes.value, 'ItemNumber') :"$" :: string AS ItemNumber,
XMLGET(at2.value, 'AttributeID') :"$" :: string Attribute_Name,
XMLGET(at2.value, 'AttributeValue') :"$" :: string Attribute_Value
FROM
cte,
LATERAL FLATTEN(XMLGET(src_xml, 'ItemsOfProduct') :"$") attributes,
lateral flatten (XMLGET(attributes.value, 'Attributes') :"$") at2

How do I extract data from xml using xml value() and openxml method?

I have an XML in this format:
<Root>
<Node-1>
<Attribute Name="Header-1"/>
<Attribute Name="Header-2"/>
<Attribute Name="Header-3">Value-1</Attribute> ------- Head_Name
<Attribute Name="Header-4">Value-2</Attribute>
<Attribute Name="Header-5">value-3</Attribute>
<Attribute Name="Id_Header">10101010</Attribute>
</Node-1>
<Node-2>
<Attribute Name="Header-6">Value-4</Attribute>
<Attribute Name="Header-7"/>
<Attribute Name="Date_Header">2011-06-04 12:51:54.0</Attribute>
<Attribute Name="Header-8"/>
<Attribute Name="Header-9">Value-5</Attribute>
<Attribute Name="Header-10">Value-6</Attribute>
<Attribute Name="Header-11">Value-7</Attribute>
<Attribute Name="Header-12"/>
<Attribute Name="Header-13">Value-8</Attribute>
<Attribute Name="Header-14">Value-9</Attribute>
<Attribute Name="Header-15">Value-10</Attribute>
</Node-2>
<Sub-Node-2/>
<Node-3>
<Sub-Node-3>
<Node-1>
<Attribute Name="Header-16">Value-11</Attribute> ------- type
<Attribute Name="Header-17"/>
<Attribute Name="Header-18">Value-12</Attribute>
<Attribute Name="Header-19">Value-13</Attribute>
<Attribute Name="Header-20">Value-14</Attribute> ------- name
</Node-1>
<Relationship type="Sub-Header-1">
<Attribute Name="Header-21">Value-15</Attribute> ------- Quantity
</Sub-Node-2>
<Node-3/>
</Sub-Node-3>
<Sub-Node-3>
<Node-1>
<Attribute Name="Header-16">Value-16</Attribute> ------- type
<Attribute Name="Header-17"/>
<Attribute Name="Header-18">Value-17</Attribute>
<Attribute Name="Header-19">Value-18</Attribute>
<Attribute Name="Header-20">Value-19</Attribute> ------- name
</Node-1>
<Relationship type="Sub-Header-1">
<Attribute Name="Header-21">Value-20</Attribute> ------- Quantity
</Relationship>
<Node-3/>
</Sub-Node-3>
<Sub-Node-3>
<Node-1>
<Attribute Name="Header-16">Value-21</Attribute> ------- type
<Attribute Name="Header-17"/>
<Attribute Name="Header-18">Value-22</Attribute>
<Attribute Name="Header-19">Value-23</Attribute>
<Attribute Name="Header-20">Value-24</Attribute> ------- name
</Node-1>
<Relationship type="Sub-Header-1">
<Attribute Name="Header-21">Value-25</Attribute> ------- Quantity
<Sub-Node-3>
</Node-3>
</Root>
I want the SQL query to display these values:
Using Openxml I tried this:
CREATE TABLE #WorkingTable
(
COLUMN1 XML
)
INSERT INTO #WorkingTable
SELECT * FROM OPENROWSET (BULK 'D:\XML_File.xml', SINGLE_BLOB) AS data
DECLARE #XML AS XML, #docHandle AS INT
SET #XML = (SELECT COLUMN1 FROM #WorkingTable)
EXEC sp_xml_preparedocument #docHandle OUTPUT, #XML
SELECT *
FROM OPENXML(#docHandle, 'Root/Node-1',2 )
WITH
(
Head_Name VARCHAR(255) 'Attribute["Name"] [3]/text()',
Type VARCHAR(255) '../Node-3/Sub-Node-3/Node-1/Attribute["Name"] [1]/text()',
Name VARCHAR(255) '../Node-3/Sub-Node-3/Node-1/Attribute["Name"] [5]/text()'
)
EXEC sp_xml_removedocument #docHandle
DROP TABLE #WorkingTable
Although I get the result, but I get it only for one node. I want result for complete nodes.
And using value() method as:
CREATE TABLE #WorkingTable
(
COLUMN1 XML
)
INSERT INTO #WorkingTable(COLUMN1) SELECT * FROM OPENROWSET (BULK 'D:\Xml_File.xml', SINGLE_BLOB) AS DATA
DECLARE #XML AS XML
SET #XML = (SELECT COLUMN1 FROM #WorkingTable)
SELECT col.value('Attribute ["Name"] [5]/text()', 'VARCHAR(255)'),
col.value('../Node-3/Sub-Node-3/Node-1/Attribute["Name"] [4]/text()','VARCHAR(255)')
FROM #XML.nodes('Root/Node-1') AS ref(col)
DROP TABLE #WorkingTable
But this gives me this error:
Msg 2203, Level 16, State 1, Line 11
XQuery [value()]: Only 'http://www.w3.org/2001/XMLSchema#decimal?', 'http://www.w3.org/2001/XMLSchema#boolean?' or 'node()*' expressions allowed as predicates, found 'xs:string'

How can I query a value in SQL Server TEXT column that contains XML (not xml column type)

I have table DOCUMENTS with:
DOCUMENTS
____________________
DOCUMENTID int
USERID int
CONTENT text
I have following XML stored in TEXT column with name CONTENT in a SQL Server database
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IDMSDocument>
<DocumentContent>
<Attribute Name="Number" GUID="{FFFFFFFF-0000-0000-0000-000000000001}">
<IDMSType>3060</IDMSType>
<Value Type="Integer">
<Value>122</Value>
</Value>
</Attribute>
<Attribute Name="Date" GUID="{FFFFFFFF-0000-0000-0000-000000000002}">
<IDMSType>3061</IDMSType>
<Value Type="DateTime">
<Date>10-09-2014</Date>
</Value>
</Attribute>
<Attribute Name="Публикуване" GUID="{CA646F55-5229-4FC5-AA27-494B25023F4E}">
<IDMSType>3062</IDMSType>
<Value Type="String">
<Value>Да</Value>
</Value>
</Attribute>
<Attribute Name="Дата" GUID="{AC0465B0-4FB4-49E2-B4FA-70901068FD9B}">
<IDMSType>3063</IDMSType>
<Value Type="DateTime">
<Date>01-10-2014</Date>
</Value>
</Attribute>
<Attribute Name="Предмет на поръчка" GUID="{04A4EC72-6F33-461F-98DD-D8D271997788}">
<IDMSType>3065</IDMSType>
<Value Type="String">
<Value>Избор на консултанти</Value>
</Value>
</Attribute>
</DocumentContent>
</IDMSDocument>
I find a way how to query dingle XML attribute in a single row from table, with heavy conversion :
DECLARE #strContent NVARCHAR(MAX);
DECLARE #xmlContent XML;
SET #strContent = (select content from DOCUMENTS where DocumentID=24);
SET #xmlContent = CAST(REPLACE(CAST(#strContent AS NVARCHAR(MAX)),'utf-8','utf-16') AS XML)
SELECT #xmlContent.query('/IDMSDocument/DocumentContent/Attribute[5]/Value/Value')
where #xmlContent.value('(/IDMSDocument/DocumentContent/Attribute[5]/Value/Value)[1]', 'nvarchar(max)') like '%search%'
I need to make query like "select all rows in table that in fifth attribute in XML have value 'search' "
For me the general problem is that column type is not XML, but I have text column with stored xml inside. when I try cast, query, value directly server return that I can use it Only with XML column.
I would be very grateful if someone suggest how to do this!
Thanks!
Kamen
SQL Server doesn't allow inline XML conversion to have functions applied, i.e.: no CAST(...).query(), use a CTE to handle the conversion:
;WITH cte AS (
SELECT DocumentID, UserID, CAST(Content AS XML) AS XMLContent
FROM Documents
)
SELECT XMLContent.query('/IDMSDocument/DocumentContent/Attribute[5]/Value/Value')
FROM cte
WHERE XMLContent.value('(/IDMSDocument/DocumentContent/Attribute[5]/Value/Value)[1]', 'nvarchar(max)') like '%search%'
One thing though: I saw Unicode (Russian?) characters in the Content column. It may be better to use utf-16 in your XML and ntext for column type.
text and ntext are also on the way out. If this is new code, use nvarchar(max)

Oracle 10 xmltype extract where clause

I'm trying to extract a given set of values from an xmlfield blob data type.
The Xml structure looks like
<?xml version="1.0" encoding="UTF-8"?>
<ItemParameters type="**XYZ" version="000000000">
<Attribute name="Item ID" elementaryType="Numeric">
<Value value="12"></Value>
</Attribute>
<Attribute name="A" elementaryType="B">
<Value value="50"></Value>
</Attribute>
</ItemParameters>
<Scales>
</Scales>
where I'd like to check if attribute name = A and elementryType = B and extract the value beneeth in this case 50.
I tried doing like
select xmltype (dynamic_data).EXTRACT ('//ItemParameters/Attribute/Value()').getVal ()
Which is not working