Solved

Looping Replace SQL SERVER 2005

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 29
Help Required 3 96
What's wrong with this T-SQL Foreign Key? 7 44
access query to sql server 3 18
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 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