Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Tuesday, December 02, 2008

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Code Snip: TSQL Search and Replace Text Field
10/21/2004 7:11:04 PM

In this SQL code snippet we go over a method to do a search and replace on text fields within a SQL database. This code came in particularly handy when converting over from DNN 1.x to DNN 2.x where the portal directories changed from a long hex based GUID to a simple integer value.

 

ALTER PROCEDURE dbo.SearchAndReplace

--here we'll accept some values for the the search string and the replace string.
@FindString varchar(8000),
@ReplaceString varchar(8000)
AS
SET NOCOUNT ON

DECLARE @TextPointer varbinary( 16 )
DECLARE @DeleteLength int
DECLARE @OffSet int

SELECT @TextPointer = textptr( MyTextField)
FROM MyTable
SET @DeleteLength = len( @FindString )
SET @OffSet = 0
WHILE ( SELECT count( * )
FROM MyTable
WHERE patindex( '%' + @FindString + '%', MyTextField) <> 0
) > 0
BEGIN
SELECT @OffSet = patindex( '%' + @FindString + '%', MyTextField) - 1
FROM MyTable
WHERE patindex( '%' + @FindString + '%', MyTextField) <> 0
UPDATETEXT MyTextField
@TextPointer
@OffSet
@DeleteLength
@ReplaceString
END
RETURN


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 5/27/2008 1:11:03 PM by Anonymous
Comments: Very helpful introduction to updating memo fields.  Just what I needed.

Thanks for posting it.
Left on 5/9/2008 10:35:13 AM by Anonymous
Comments: Gawd, get tired of people posting code that doesn't work!
Left on 4/15/2007 6:29:38 PM by Anonymous
Comments:
No ratings available.
Left on 7/7/2006 4:04:06 PM by Anonymous
Comments: seems to be stuck on updating first record....adding "PRINT @offset" before the end of the loop returns the same number again and again
Left on 7/7/2006 3:28:42 PM by Anonymous
Comments: doesn't work.  proc runs and just hangs....never does update
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970