?
Solved

Looping Replace SQL SERVER 2005

Posted on 2008-10-22
5
Medium Priority
?
395 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 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

765 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