SQL Server query xml column -


i need pull values xml column. table contains 3 fields 1 being xml column below:

transid int, place varchar(20), custom xml

the xml column structured following:

<fields>   <field>     <id>9346-00155d1c204e</id>     <transactioncode>0710</transactioncode>     <amount>5.0000</amount>   </field>   <field>     <id>a6f0-ba07ef3a7d43</id>     <transactioncode>0885</transactioncode>     <amount>57.9000</amount>   </field>    <field>     <id>9bda-7858fd182z3c</id>     <transactioncode>0935</transactioncode>     <amount>25.85000</amount>   </field>  </fields> 

i need able query xml column , return value <amount> if there <transaction code> = 0935. note: there records transaction code isn’t present, won't exist in same record twice.

this simple, i’m having problem returning <amount> value <transaction code> = 0935.

you can try way :

declare @transcode varchar(10) = '0935' select field.value('amount[1]', 'decimal(18,5)') amount yourtable t      outer apply t.custom.nodes('/fields/field[transactioncode=sql:variable("@transcode)"]') x(field) 

alternatively, can put logic filtering field transactioncode in sql where clause instead of in xpath expression, :

declare @transcode varchar(10) = '0935' select field.value('amount[1]', 'decimal(18,5)') amount yourtable t      outer apply t.custom.nodes('/fields/field') x(field) field.value('transactioncode[1]', 'varchar(10)') = @transcode 

sql fiddle demo


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -