| 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 fieldA simplified table structure: DatatableField1 nvarchar(50)MatchTableLookFor 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 replacewithI 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 Datatableset 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. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-05 : 09:08:43
|
quote: Originally posted by webfred This?update Datatableset 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 MatchtableMadhivananFailing to plan is Planning to fail |
 |
|
|
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 fieldA simplified table structure: DatatableField1 nvarchar(50)MatchTableLookFor 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 replacewithI 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 isdeclare @Datatable table(Field1 nvarchar(50))insert into @Datatable (Field1)select 'testing for this' union allselect 'testing for this to' declare @MatchTable table(LookFor nvarchar(50),Replacewith nvarchar(50))insert into @MatchTable (LookFor,Replacewith)select 'for','with' union allselect '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)MadhivananFailing to plan is Planning to fail |
 |
|
|
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. |
 |
|
|
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 tableMadhivananFailing to plan is Planning to fail |
 |
|
|
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 |
 |
|
|
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 MadhivananFailing to plan is Planning to fail |
 |
|
|
|