How to find and replace data by xquery in below xml. below xml is stored in a table column whose data type is xml.
i will have a store procedure where i will send line item and xfund code as parameter. i want to search group key by line item & replace xfund code in xml with new xfundcode will be provided to SP.
speciall see this line where lineitem and xfund code is there concatinated by ~ sign.
<GroupKey>Consensus Model~Total Revenue~TRIN~NBM~~1~ZB-P1</GroupKey>
- GroupKey field has many data concatinated by ~ sign.
- line item and xfundcode is 2nd and 3rd element in group key separated by ~sign.
- i want to search line item in group key and replace old xfundcode by new xfundcode.
- Lineitem and new xfundcode will be pass to SP as parameter
in group key first element is Section, second element is Line Item and second element is xfundcode. all are separated by ~ sign.
the below xml is stored in table column.
so please share a query by which i can use to find line item in groupkey in the stored xml in table column. if line item found then xfund code in group key will be replaced with a xfund code which will be passed to SP and xml will be updated in table column.
<?xml version="1.0" encoding="utf-8"?>
<PWR_ViewAll>
<dgvViewAll_Vertical>
<Section_x0020_>ZB-P1</Section_x0020_>
<LineItem>B. Riley FBR Inc.</LineItem>
<Revise_x0020_Date>08-21-2020</Revise_x0020_Date>
<GroupKey>Consensus Model~Total Revenue~TRIN~NBM~~1~ZB-P1</GroupKey>
</dgvViewAll_Vertical>
<dgvViewAll_Vertical>
<Section_x0020_>CL</Section_x0020_>
<LineItem>Deutsche Bank</LineItem>
<Revise_x0020_Date>02-28-2020</Revise_x0020_Date>
<GroupKey>Consensus Model~Total Revenue~TRIN~NBM~~1~CL</GroupKey>
</dgvViewAll_Vertical>
</PWR_ViewAll>
Thanks
EDIT
Sir thanks for your link but my scenario is bit different i want to search data in group key
<GroupKey>Consensus Model~Total Revenue~TRIN~NBM~~1~ZB-P1</GroupKey> has few data in it concatenated by ~ sign.
so i want to search line item in group key field. if match then replace it xfund code by new xfund code
1) first element in GroupKey is section, 2nd one is line item, 3rd one is xfundcode and rest element in group key not known to me.
so i want to search on group key in xml and if line item found then replace xfundcode. see my code here. now tell me how could i replace xfundcode in xml and update that xml in table.
declare @xml xml
declare @li varchar(max) ='Segment Detail'
declare @newXfundCode varchar(max) ='TEST'
CREATE TABLE #tmpData (id INT, xmldata xml)
INSERT INTO #tmpData(id,xmldata) values (1,N'<?xml version="1.0" encoding="utf-16"?>
<PWR_ViewAll>
<dgvViewAll_Vertical>
<Section_x0020_>ZB-P1</Section_x0020_>
<LineItem>B. Riley FBR Inc.</LineItem>
<Revise_x0020_Date>08-21-2020</Revise_x0020_Date>
<GroupKey>Consensus Model~Total Revenue~TRIN~NBM~~1~ZB-P1</GroupKey>
</dgvViewAll_Vertical>
<dgvViewAll_Vertical>
<Section_x0020_>CL</Section_x0020_>
<LineItem>Deutsche Bank</LineItem>
<Revise_x0020_Date>02-28-2020</Revise_x0020_Date>
<GroupKey>Segment Detail~Total Revenue~TEST~NBM~~1~CL</GroupKey>
</dgvViewAll_Vertical>
</PWR_ViewAll>')
SELECT @xml=xmldata from #tmpData where ID=1
SELECT col.value('(GroupKey/text())[1]', 'VARCHAR(MAX)')
FROM @xml.nodes('/PWR_ViewAll/dgvViewAll_Vertical') AS tab (col)
WHERE CHARINDEX(@li, col.value('(GroupKey/text())[1]', 'VARCHAR(MAX)'))>0
Drop table #tmpData