Solved

Looping Replace SQL SERVER 2005

Posted on 2008-10-22
5
383 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
  • 3
  • 2
5 Comments
 
LVL 59

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 59

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now