Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Replace substring of all nText fields

Status
Not open for further replies.

esdaniel

IS-IT--Management
Jun 10, 2001
14
0
0
GB
Does anyone have a script to replace a string within an nText field with another string for all nText fields in a SQL Svr 2K database?

Ed.
 
CREATE PROCEDURE SP_REPLACEALLTEXT (@TableName varchar(100), @FindText varchar(100), @ReplaceText varchar(100)) AS
DECLARE @SQL nvarchar(4000)
SET @SQL='UPDATE '+@TableName+' SET '
SELECT @FindText=REPLACE(@FindText, '''', ''''''), @ReplaceText=REPLACE(@ReplaceText, '''', '''''')
SELECT @SQL=@SQL+'['+COLUMN_NAME+']=REPLACE(['+COLUMN_NAME+'], '+''''+@FindText+''', '+''''+@ReplaceText+'''), ' FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE IN ('varchar','char')
AND '['+TABLE_SCHEMA+'].['+TABLE_NAME+']'=@TableName

AND TABLE_NAME not like '%Sys_%'

IF @@ROWCOUNT > 0 BEGIN
SET @SQL=LEFT(@SQL, LEN(@SQL)-1)
PRINT(@SQL)
EXEC(@SQL)
PRINT @TableName+': done'
END
GO

EXEC SP_MSFOREACHTABLE 'EXEC SP_REPLACEALLTEXT ''?'', '''''''', ''`'''
DROP PROCEDURE SP_REPLACEALLTEXT


This will replace ' with ` in varchar and char columns. So you just need to change the data types, and the search and replace criteria. Also the AND TABLE_NAME not like '%Sys_%' part to exclude the tables u dont want to do.


regards
 
Sadly not - you can't use the REPLACE function with an nText column.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top