Solved

Select REPLACE in XML

Posted on 2008-10-16
8
950 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
  • 4
  • 4
8 Comments
 
LVL 59

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 59

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 59

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 59

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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