August 31, 2009 at 10:45 am
I have an ntext column that contains the full text of an rdl object. I need to update every occurrence of Parameters!AnyParm.Value with Parameters!AnyParm.Label.
I could use Replace, Stuff, PatIndex, etc. to replace the first occurrence. Any help on how to change every occurrence in the column?
"Parameters!" is always there. "Value" is always there. The "AnyParm" name will be different and occurs up to four or five times per row.
Thanks.
August 31, 2009 at 11:23 am
Scott Solice (8/31/2009)
I have an ntext column that contains the full text of an rdl object. I need to update every occurrence of Parameters!AnyParm.Value with Parameters!AnyParm.Label.I could use Replace, Stuff, PatIndex, etc. to replace the first occurrence. Any help on how to change every occurrence in the column?
"Parameters!" is always there. "Value" is always there. The "AnyParm" name will be different and occurs up to four or five times per row.
Thanks.
text and ntext are a pain, which is why they brought out the varchar and nvarchar(max) types.
the fast way would be to simply cast/convert to nvarchar(max), and use the replace function:
both snippets below repalce every occurance of the string with the new one, for every row, if it iexists.
UPDATE YOURTABLE
SET YOURNTEXTCOLUMN = REPLACE(CONVERT(varchar(max),YOURNTEXTCOLUMN ),'Parameters!AnyParm.Value', 'Parameters!AnyParm.Label')
the oldstyle way to do it was using this cursor as an example and using the UPDATETEXT function
DECLARE
@ItemID INT,
@ptr BINARY(16), @idx INT,
@oldString varchar(250),
@newString varchar(250),
@lenOldString int
SET @oldString ='a href='
SET @newString ='a href="MyPath/'
SET @lenOldString=datalength(@oldString)
--change the table Announcements to your table
--change the column Description to your columnname
SET @ItemID = 0
--YOU must find and replace three object names:
--ItemId--the PK of the table
--Announcements.Description --the TEXT field to manipulate
--Announcements --The Table Name to Manipulate
SELECT TOP 1 @ItemID = ItemID, @idx = PATINDEX(@oldString,Announcements.Description)-1
FROM Announcements
WHERE PATINDEX(@oldString,Announcements.Description) > 0
WHILE @ItemID > 0
BEGIN
SELECT @ptr = TEXTPTR(Announcements.Description)
FROM Announcements
WHERE ItemID = @ItemID
UPDATETEXT Announcements.Announcements.Description @ptr @idx @lenOldString @newString
SET @ItemID = 0
SELECT TOP 1 @ItemID = ItemID, @idx = PATINDEX(@oldString,Announcements.Description)-1
FROM Announcements
WHERE ItemID > @ItemID
AND PATINDEX(@oldString,Announcements.Description) > 0
END
Lowell
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply