Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Partial Search and replace from within sp on table

Author  Topic 

miketl
Starting Member

2 Posts

Posted - 2009-08-05 : 08:29:39
Looking for an efficient way to do a partial search and replace on a field in one table with a data in another field

A simplified table structure:

Datatable
Field1 nvarchar(50)


MatchTable
LookFor nvarchar(50)
Replacewith nvarchar(50)

Idea is to see if the value in lookfor exists (even partially) in Field1 for all rows in datatable and if it does replace it with the value from replacewith

I realize this can be done with a cursor and looping through each row in matchTable to see if lookfor exists in field1 in datatable but was hoping their might be a more efficient way someone smarter than I knows...

Purpose of the code is a bulk import routine that needs to normalise some data.. e.g things like Avenue to Ave

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-05 : 09:03:52
This?
update Datatable
set Field1=replace(Field1,(select LookFor from MatchTable),(select Replacewith from MatchTable))


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-05 : 09:08:43
quote:
Originally posted by webfred

This?
update Datatable
set Field1=replace(Field1,(select LookFor from MatchTable),(select Replacewith from MatchTable))


No, you're never too old to Yak'n'Roll if you're too young to die.


It will throw error if there is more than a row in the Matchtable


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-05 : 09:09:06
quote:
Originally posted by miketl

Looking for an efficient way to do a partial search and replace on a field in one table with a data in another field

A simplified table structure:

Datatable
Field1 nvarchar(50)


MatchTable
LookFor nvarchar(50)
Replacewith nvarchar(50)

Idea is to see if the value in lookfor exists (even partially) in Field1 for all rows in datatable and if it does replace it with the value from replacewith

I realize this can be done with a cursor and looping through each row in matchTable to see if lookfor exists in field1 in datatable but was hoping their might be a more efficient way someone smarter than I knows...

Purpose of the code is a bulk import routine that needs to normalise some data.. e.g things like Avenue to Ave


One of the methods is



declare @Datatable table(Field1 nvarchar(50))
insert into @Datatable (Field1)
select 'testing for this' union all
select 'testing for this to'


declare @MatchTable table(LookFor nvarchar(50),Replacewith nvarchar(50))
insert into @MatchTable (LookFor,Replacewith)
select 'for','with' union all
select 'to','no'

declare @replace varchar(8000),@with varchar(8000),@sql varchar(8000)
select @replace='',@with ='',@sql=''
select
@replace =@replace +'replace(',
@with =@with +''''+Lookfor +''','''+Replacewith+'''),' from @MatchTable
select @replace=@replace +'''~!@#'',',@with =SUBSTRING(@with,1,len(@with)-1)
select @sql=@sql+REPLACE('select '+@replace+@with,'~!@#',Field1)+' union all ' from @Datatable
select @sql=SUBSTRING(@sql,1,len(@sql)-10)
select * from @Datatable
exec(@sql)



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-05 : 09:20:20
Madhi the sorcerer!
What does '~!@#'?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-05 : 09:25:13
quote:
Originally posted by webfred

Madhi the sorcerer!
What does '~!@#'?


No, you're never too old to Yak'n'Roll if you're too young to die.



Thanks

It is temporary value which gets replace by actual data from @Datatable table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

miketl
Starting Member

2 Posts

Posted - 2009-08-06 : 08:02:19
Thanks madhivanan, not something I would have ever come up with.. looks the goods.. I will do some testing on it for load, but I think it will be about as fast as it can be done given what is required
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-06 : 08:32:45
quote:
Originally posted by miketl

Thanks madhivanan, not something I would have ever come up with.. looks the goods.. I will do some testing on it for load, but I think it will be about as fast as it can be done given what is required



Ok. Post the test result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -