Solved

Select REPLACE in XML

Posted on 2008-10-16
8
1,013 Views
Last Modified: 2010-04-21
I'm not sure if this is do-able.

I'm doing a select on a result set that is XML data.
select replace([theDate], 'images1', 'images') from theTemp

I need to do it with a replace statement and am getting
Argument data type xml is invalid for argument 1 of replace function.
0
Comment
Question by:lrbrister
[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
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22730312
http://technet.microsoft.com/en-us/library/ms345122.aspx#sql2k5_xqueryintro_topic13

See the above.  You will need to use XML methods like XQuery example in link like:
[theDate].replace(...)

Hope this helps.
0
 

Author Comment

by:lrbrister
ID: 22730392
mwvisa1:,
  I am way out of my depth here.  You have a select statement example?
For XQuery or XML methods you may as well be speeking ancient Aramaic to me.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22730642
Sorry about that.  I needed a little more coffee in me anyway.  Out west right now, so been up early in this time zone. :)

Do something like this function:
(maybe there is another way, but with the method I suggested it doesn't like mutating the XML right in the select as it uses a SET command; therefore, I first create the below in code snippet then usage is as below)

SELECT dbo.fn_ChangeXMLText(theData, 'images1', 'images')
FROM theTemp
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_ChangeXMLText]
(
	@theXml XML,
    @oldText nvarchar(50),
    @newText nvarchar(50)
)
RETURNS XML
AS
BEGIN
	SET @theXml.modify('replace value of (//text()[.=sql:variable("@oldText")])[1] with sql:variable("@newText")')
	RETURN @theXml
END

Open in new window

0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

Author Comment

by:lrbrister
ID: 22730966
mwvisa1:
I set up some variables and ran it below.  No replace is taking place.

----CODE----
declare @theXml xml
declare @oldText nvarchar(50)
declare @newText nvarchar(50)
set @theXML = '<images1>http://www.mydomain.com/luxury_real_estate/florida/broward/fort_lauderdale/images/F868140_01.jpg</images1>'
set @oldText = 'images1'
set @newText = 'images'
SELECT dbo.fn_ChangeXMLText(@theXML, @oldText, @newText)
FROM theTemp


----RESULT SET-----
<images1>http://www.mydomain.com/luxury_real_estate/florida/broward/fort_lauderdale/images/F868140_01.jpg</images1>
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22732264
images1 is a node in your example and not text.  I am trapping on text.  If you want to replace nodes, change the function to be //images1.
0
 

Author Comment

by:lrbrister
ID: 22732682
So...???
SET @theXml.modify('replace value of (//images1()[.=sql:variable("@oldText")])[1] with sql:variable("@newText")')
      RETURN @theXml
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22737906
Sorry, in conference all day.

I am not certain you can do that for the node name itself.  I will have to do research to make sure.

Regards,
Kevin
0
 

Author Closing Comment

by:lrbrister
ID: 31506690
You put me on the right track.  Will post final code later
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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