?
Solved

How do I partly modify xml column attribute like native sql replace

Posted on 2010-11-23
8
Medium Priority
?
718 Views
Last Modified: 2012-05-10
Hi experts,

all I want to do is modify a XML Attribute value within a XML column type like the ordinary replace function.
These are my rows within the xml column

<test attr="http://www.link.com/abc"/>
<test attr="http://www.link.com/def"/>
<test attr="www.link.com/xyz"/>

Open in new window


UPDATE dbo.test
SET    testCol = replace(testCol, 'www.link.com', 'www.mylink.com')
WHERE  testCol LIKE '%www.link.com%';

Open in new window


But modifying a xml column only works through

testCol.modify('replace value of xpath-expression with new-value')

Open in new window


where I cannot use any native replace function nor xpath translate function within "new-value".

This is my needed result.
<test attr="http://www.mylink.com/abc"/>
<test attr="http://www.mylink.com/def"/>
<test attr="www.mylink.com/xyz"/>

Open in new window


Thanks in advance for your help.
0
Comment
Question by:arthrex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34206486
Is this sufficient? I don't think you'll be able to do it in a single query and you will probably have to create a cursor to replace the whole table.
declare @temp varchar(128)

-------------------------------------------------------------------------------------
--  Case 1
-------------------------------------------------------------------------------------
declare @sample1 xml
set @sample1 = '<test attr="http://www.link.com/abc"/>'

set @temp = @sample1.value('(/test/@attr)[1]', 'varchar(128)')
set @temp = REPLACE(@temp, 'www.link.com', 'www.mylink.com')
set @sample1.modify('replace value of (/test/@attr)[1] with sql:variable("@temp")')

select @sample1

-------------------------------------------------------------------------------------
--  Case 2
-------------------------------------------------------------------------------------
declare @sample2 xml
set @sample2 = '<test attr="http://www.link.com/def"/>'

set @temp = @sample2.value('(/test/@attr)[1]', 'varchar(128)')
set @temp = REPLACE(@temp, 'www.link.com', 'www.mylink.com')
set @sample2.modify('replace value of (/test/@attr)[1] with sql:variable("@temp")')

select @sample2

-------------------------------------------------------------------------------------
--  Case 3
-------------------------------------------------------------------------------------
declare @sample3 xml
set @sample3 = '<test attr="www.link.com/xyz"/>'

set @temp = @sample3.value('(/test/@attr)[1]', 'varchar(128)')
set @temp = REPLACE(@temp, 'www.link.com', 'www.mylink.com')
set @sample3.modify('replace value of (/test/@attr)[1] with sql:variable("@temp")')

select @sample3

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34206742
Maybe you won't need the cursor  :)

Make sure you size the varchar accordingly to fit the textual length of your xml field:
update Test
set data = CAST(replace(data.value('(/test/@attr)[1]', 'varchar(128)'), 'www.link.com','www.mylink.com') as xml)

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34206784
Disregard, the last post is not valid.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 34207973
I fall back to my previous cursor comment:
declare cur cursor
	for select data from test
	for update of data

declare @sample1 xml
declare @temp varchar(128)
	
open cur;

fetch next from cur
	into @sample1
	
while @@FETCH_STATUS = 0
begin
	set @temp = @sample1.value('(/test/@attr)[1]', 'varchar(128)')
	set @temp = REPLACE(@temp, 'www.link.com', 'www.mylink.com')
	set @sample1.modify('replace value of (/test/@attr)[1] with sql:variable("@temp")')
	
	update Test set data = @sample1
		where current of cur
	
	fetch next from cur
		into @sample1
end

close cur
deallocate cur

select * from Test

Open in new window

0
 

Author Comment

by:arthrex
ID: 34208614
Hello kaufmed,

are you sure that it is not possible to solve it within a single query?
I also thought about a procedure in the first place but I need to call the update without modifying the procedures on the database ;)

But thank you very much for the cursor example.
0
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 1600 total points
ID: 34220982
Hi,

check out this
create table #table1
(
col xml
)
insert into #table1
values
('<test attr="http://www.link.com/abc"/>')
insert into #table1
values
('<test attr="http://www.link.com/def"/>')
insert into #table1
values
('<test attr="www.link.com/xyz"/>')

select * from #table1

UPDATE dbo.#table1
SET    col = replace(cast(Col as varchar(max)), 'www.link.com', 'www.mylink.com')
where cast(Col as varchar(max)) LIKE '%www.link.com%';

select * from #table1

Open in new window

0
 

Author Closing Comment

by:arthrex
ID: 34256105
So simple ;)

Still one question. Is mssql2005 autocasting the replaced varchar(max) back into valid xml (
set col = replace(cast(col as varchar(max)),'','') )?

Best regards and thanks!
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34256557
Hi.....

Yes....if mssql can not do casting then it will throw an error....

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question