Solved

Looping Replace SQL SERVER 2005

Posted on 2008-10-22
5
390 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

766 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