Solved

Looping Replace SQL SERVER 2005

Posted on 2008-10-22
5
393 Views
Last Modified: 2013-11-10
I have a table 't_companies' and a table 't_thesaurus'.  t_companies has  'id', 'company_name', 'new_company_name' columns. t_thesaurus has 'find_string', 'replace_string'.

What I'd like to do is replace all instances of a word in t_thesaurus.find_string with the 'replace_string'.

ex.  
t_companies.company name = 'smith bros and co'
 t_thesaurus.find_string has two records that could fix the company name, 'bros' and 'co', to be replaced by values of 'brothers' and 'company', respectively.
in the end, i'd like t_companies.new_company_name = 'smith brothers and company'.

any easy ideas?  i'm using sql server 2005.  it would be great to be able to do in SSIS, but i don't have any idea the complexity of SSIS vs T-SQL.
0
Comment
Question by:cmgtech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22782205
Something like this (see code snippet) should work if new_company_name starts out NULL or equal to company_name.  If not, you could use an update statement to set the new_company_name equal to company_name before beginning the replace process; however, what I am doing is using coalesce to use company_name when new_company_name is NULL.  Therefore on next iteration when new_company_name has been replaced once already, you will use the new replaced value in the new_company_name field instead of the original one that hasn't been replaced for the previous word.

Here are the references for my solution:
http://msdn.microsoft.com/en-us/library/ms180169.aspx
http://msdn.microsoft.com/en-us/library/ms186862.aspx
DECLARE @find nvarchar(100), @replace nvarchar(100)
 
DECLARE thesaurus_cursor CURSOR FOR 
SELECT find_string, replace_string
FROM t_thesaurus
 
OPEN thesaurus_cursor
 
FETCH NEXT FROM thesaurus_cursor 
INTO @find, @replace
 
WHILE @@FETCH_STATUS = 0
BEGIN
	UPDATE t_companies
	SET new_company_name = replace(coalesce(new_company_name, company_name, ''), @find, @replace)
END 
 
CLOSE thesaurus_cursor
DEALLOCATE thesaurus_cursor

Open in new window

0
 

Author Comment

by:cmgtech
ID: 22782293
On 88K records, it has taken 20 minutes and counting to run.  Does that sound right?  It's running on a blade server with 4 gig ram.

Does @@FETCH_STATUS have to be incremented?
0
 

Author Comment

by:cmgtech
ID: 22782385
I took it down to 100 records and it still takes forever.  Is there something in here causing an infinite loop?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22782426
Sorry copy and past error.

FETCH NEXT FROM thesaurus_cursor
INTO @find, @replace

Should appear within the while loop, so like this:
DECLARE @find nvarchar(100), @replace nvarchar(100)
 
DECLARE thesaurus_cursor CURSOR FOR 
SELECT find_string, replace_string
FROM t_thesaurus
 
OPEN thesaurus_cursor
 
FETCH NEXT FROM thesaurus_cursor 
INTO @find, @replace
 
WHILE @@FETCH_STATUS = 0
BEGIN
	UPDATE t_companies
	SET new_company_name = replace(coalesce(new_company_name, company_name, ''), @find, @replace)
 
-- get the next thesaurus replacement
FETCH NEXT FROM thesaurus_cursor 
INTO @find, @replace
END 
 
CLOSE thesaurus_cursor
DEALLOCATE thesaurus_cursor

Open in new window

0
 

Author Closing Comment

by:cmgtech
ID: 31509009
Perfect, small issue with the first script, but the second was great.

Thanks,

Mark
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question