Important alert: (current site time 7/15/2013 7:49:56 PM EDT)
 

VB icon

A REPLACE function for a TEXT field

Email
Submitted on: 11/29/2001 6:26:04 PM
By: J Shortland 
Level: Intermediate
User Rating: By 7 Users
Compatibility: SQL Server 7.0
Views: 36153
(About the author)
 
     Generically, the Replace function in SQL Server doesn't support replacing a particular string with another within a TEXT field; this can.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: A REPLACE function for a TEXT field
-- Description:Generically, the Replace function in SQL Server 
doesn't support replacing a particular string 
with another within a TEXT field; this can.
-- By: J Shortland
--
-- Assumes:Replace <TableName> with the name of the table with the TEXT field
Replace	<ColumnName> with the name of the TEXT field
Replace <String1> with the string to look for
Replace <String2> with the string to replace with
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=367&lngWId=5--for details.--**************************************

/*********************************************************************
--			
-- Name			ReplaceBlob
-- Date Created:	30/11/2001
-- Created By:		J Shortland
-- Description:		Generically, the Replace function in SQL Server 
--			doesn't support replacing a particular string 
--			with another within a TEXT field; this can.
--			
-- Steps Performed:	Step 01 : Obtain the handle for the TEXT field to be processed
--			Step 02 : Set the search string
--			Step 03 : Set the replacement string
--			Step 04 : Set the delete length of string to be removed from the TEXT field
--			Step 05 : Initialise the starting position for the update
--			Step 06 : Loop thru table until all instances of the search string are replaced
--			Step 06a : Set the starting position for the update
--			Step 06b : Replace the search string with the replacement string
--			
-- Instructions:	Replace <TableName> with the name of the table with the TEXT field
--			Replace	<ColumnName> with the name of the TEXT field
--			Replace <String1> with the string to look for
--			Replace <String2> with the string to replace with
--			
-- History:		
-- |-------------------------------------------------------------------|
-- | 30/11/2001		JHS		Creation
-- |-------------------------------------------------------------------|
--			
-- ********************************************************************/
		
set nocount on	
		
declare		@TextPointer	varbinary( 16 )	-- Stores the handle for the TEXT field to be processed
declare		@ReplaceStr	varchar( 8000 )	-- Stores the replacement string
declare		@SearchStr	varchar( 8000 )	-- Stores the search string
declare		@DeleteLength	int		-- Stores the length of string to be removed
declare		@OffSet		int		-- Stores the starting position for the update
		
-- Step 01 : Obtain the handle for the TEXT field to be processed
select		@TextPointer	= textptr( <ColumnName> )
from		<TableName>
		
-- Step 02 : Set the search string
set		@SearchStr	= <String1>
		
-- Step 03 : Set the replacement string
set		@ReplaceStr	= <String2>
		
-- Step 04 : Set the delete length of string to be removed from the TEXT field
set		@DeleteLength	= len( @SearchStr )
		
-- Step 05 : Initialise the starting position for the update
set		@OffSet	= 0
		
-- Step 06 : Loop thru table until all instances of the search string are replaced
while		(	select	count( * )
			from	<TableName>
			where	patindex( '%' + @SearchStr + '%', <ColumnName> ) <> 0
		) > 0
begin		
		-- Step 06a : Set the starting position for the update
		select		@OffSet = patindex( '%' + @SearchStr + '%', <ColumnName> ) - 1
		from		<TableName>
		where		patindex( '%' + @SearchStr + '%', <ColumnName> ) <> 0
		
		-- Step 06b : Replace the search string with the replacement string
		updatetext	<TableName>.<ColumnName>	-- table_name.dest_column_name
				@TextPointer			-- dest_text_ptr
				@OffSet				-- insert_offset
				@DeleteLength			-- delete_length
				@ReplaceStr			-- inserted_data
end		


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/30/2001 9:15:25 AMCharles Kincaid

Ever notice how few comments there are in the SQL area?

This could come in very handy for processing large text fields. 4 from me.
(If this comment was disrespectful, please report it.)

 
1/24/2002 4:07:37 AMSebastiano Pallaro

Seems really good. This can be really useful. Now I'll try it.

4 From me
(If this comment was disrespectful, please report it.)

 
2/25/2003 11:21:21 AMChris Kesler

It appears this only works with TEXT, NTEXT and IMAGE columns as I have a VarChar Field I'd like to search and replace on. This is the error I get: Only text, ntext, and image columns are valid with the TEXTPTR function. Do you know of a workaround for this issue?

Thanks,

Chris
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.