Important alert: (current site time 7/15/2013 7:50:07 PM EDT)
 

VB icon

A REPLACE function for a TEXT field as a Stored Procedure

Email
Submitted on: 1/20/2002 4:12:18 PM
By: J Shortland 
Level: Intermediate
User Rating: Unrated
Compatibility: SQL Server 7.0
Views: 23860
(About the author)
 
     This is a Stored Procedure version of my original submission: A REPLACE function for a TEXT field http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=367&lngWId=5
 
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 as a Stored Procedure
-- Description:This is a Stored Procedure version of my original submission: A REPLACE function for a TEXT field
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=367&lngWId=5
-- By: J Shortland
--
-- Inputs:@TableName	varchar( 255 )	-- Table name containing Blob field
@ColumnName	varchar( 255 )	-- Column name of Blob field
@SearchStr	varchar( 255 )	-- Search string
@ReplaceStr	varchar( 255 )	-- Replacement string
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=395&lngWId=5--for details.--**************************************

create procedure [dbo].[ReplaceBlob]	(	@TableName	varchar( 255 ),	-- Table name containing Blob field
						@ColumnName	varchar( 255 ),	-- Column name of Blob field
						@SearchStr	varchar( 255 ),	-- Search string
						@ReplaceStr	varchar( 255 )	-- Replacement string
					)	
as
/*********************************************************************
--			
-- Name			ReplaceBlob
-- Date Created:	21/01/2002
-- 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 : Construct & assign the static Replace function to the local variable
--			Step 02 : Replace the meta-tag '<TableName>' with the parameter '@TableName'
--			Step 03 : Replace the meta-tag '<ColumnName>' with the parameter '@ColumnName'
--			Step 04 : Replace the meta-tag '<SearchStr>' with the parameter '@SearchStr'
--			Step 05 : Replace the meta-tag '<ReplaceStr>' with the parameter '@ReplaceStr'
--			Step 06 : Display the compiled SQL statement
--			Step 07 : Execute the compiled SQL statement
--			
-- History:		
-- |-------------------------------------------------------------------|
-- | 21/01/2002		JHS		Creation
-- |-------------------------------------------------------------------|
--			
-- ********************************************************************/
set nocount on
declare	@SQL varchar( 8000 )	-- Stores the SQL statement to be executed
-- Step 01 : Construct & assign the static Replace function to the local variable
set @SQL =
'
/* -- Start of Replace Script -- */
set nocount on
	
declare		@TextPointer	varbinary( 16 )	-- Stores the handle for the TEXT field to be processed
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 delete length of string to be removed from the TEXT field
set		@DeleteLength	= len( ''<SearchStr>'' )
		
-- Step 03 : Initialise the starting position for the update
set		@OffSet	= 0
		
-- Step 04 : 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 04a : Set the starting position for the update
 		select		@OffSet = patindex( ''%<SearchStr>%'', <ColumnName> ) - 1
 		from		<TableName>
 		where		patindex( ''%<SearchStr>%'', <ColumnName> ) <> 0
 		
 		-- Step 04b : 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
set nocount off
/* -- End of Replace Script -- */
'
-- Step 02 : Replace the meta-tag '<>' with the parameter '@TableName'
set	@SQL = replace( @SQL, '<TableName>', @TableName )
-- Step 03 : Replace the meta-tag '<ColumnName>' with the parameter '@ColumnName'
set	@SQL = replace( @SQL, '<ColumnName>', @ColumnName )
-- Step 04 : Replace the meta-tag '<SearchStr>' with the parameter '@SearchStr'
set	@SQL = replace( @SQL, '<SearchStr>', @SearchStr )
-- Step 05 : Replace the meta-tag '<ReplaceStr>' with the parameter '@ReplaceStr'
set	@SQL = replace( @SQL, '<ReplaceStr>', @ReplaceStr )
-- Step 06 : Display the compiled SQL statement
print	@SQL
-- Step 07 : Execute the compiled SQL statement
exec	( @SQL )
set nocount off


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


 There are no comments on this submission.
 

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.