| Author |
Topic |
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-22 : 14:57:11
|
| (sr)how do i get rid of the brackets without using any function?i was reading performance tuning. it says its better not to use functions as it slows down the SP. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-22 : 15:00:23
|
| But even built-in REPLACE is a function.Are you referring to USER DEFINED FUNCTIONS?Peter LarssonHelsingborg, Sweden |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-22 : 15:03:31
|
| yeah i know.then how are we suppose to avoid it if we must use it.is the article wrong or what? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-22 : 15:11:32
|
| It depends on what the function does.There are a lot of examples where UDFs are the only way to go in order to accomplish a task.Peter LarssonHelsingborg, Sweden |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-22 : 15:12:11
|
| not UDFim talking about replace, substring, left |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-11-22 : 15:23:50
|
| Any data transformations performed in sql server will require the use of some built-in functions. The ones you mentioned above can be extremely efficient. Excpetions can be if you are using these functions as part of your JOIN or WHERE criteria. If you use a column (that is indexed) in an expression the optimizer may not use the index as part of the execution plan.Be One with the OptimizerTG |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-22 : 15:55:44
|
| update tableset field = replace(field, '(', '')where field in ('(sr)', '(III)')update tableset field = replace(field, ')', '')where field in ('(sr)', '(III)')how can i get rid of both brackets using only 1 update. |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-22 : 16:00:17
|
| update tableset field = replace(replace(field, '(sr)', 'sr'), '(III)', 'III')where field in ('(sr)', '(III)')But this will be much betterupdate tableset field = 'sr'where field = '(sr)'update tableset field = 'III'where field = '(III)' |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-11-22 : 16:10:02
|
I'm not sure your two updates will be "much better" than the single update with the nested replace. But here is another option that might give best of both:update [table]set field = case when field = '(sr)' then 'sr' when field = '(III)' then 'III' endwhere field in ('(sr)', '(III)')Be One with the OptimizerTG |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-22 : 16:18:48
|
Yeah, "much" was a very relative term there Given that this is probably a once off update it really shouldn't matter?! |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-22 : 17:20:38
|
| what about if i have a field with "Larry (JR)" and "larry ('III')"i have the following code. will take any suggestion to make it faster.update [table]set field = case when field like '% (jr)' then replace(field, '(jr)', 'jr') when field like '% (iii)' then replace(field, '(iii)', 'iii')endwhere field like '% (jr)'or field like '% (iii)' |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-22 : 20:10:46
|
quote: Originally posted by funketekun what about if i have a field with "Larry (JR)" and "larry ('III')"i have the following code. will take any suggestion to make it faster.update [table]set field = case when field like '% (jr)' then replace(field, '(jr)', 'jr') when field like '% (iii)' then replace(field, '(iii)', 'iii')endwhere field like '% (jr)'or field like '% (iii)'
In this case, the WHERE clause is very inefficient because it means SQL Server has to scan the entire table, so the speed of the replace function calls is pretty irrelevant. |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-23 : 08:36:45
|
| snSQL, how can i tune it up? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-23 : 08:40:04
|
| UPDATE [Table] SET [Column] = REPLACE(REPLACE([Column], '(', ''), ')', '')WHERE [Column] LIKE '%(%' OR [Column] LIKE '%)%'Peter LarssonHelsingborg, Sweden |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-23 : 09:07:42
|
| peso,i only want to remove the brackets that has jr, sr, ii, iii, iv inside. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-23 : 09:10:37
|
Tadaa!UPDATE [Table]SET [Column] = REPLACE(REPLACE([Column], '(', ''), ')', '')WHERE [Column] LIKE '%(jr)%' OR [Column] LIKE '%(sr)%' OR [Column] LIKE '%(ii)%' OR [Column] LIKE '%(iii)%' OR [Column] LIKE '%(iv)%'Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-23 : 09:12:00
|
Or more specificUPDATE [Table]SET [Column] = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE([Column], '(jr)', 'jr'), '(sr)', 'sr'), '(ii)', 'ii'), '(iii)', 'iii'), '(iv)', 'iv')WHERE [Column] LIKE '%(jr)%' OR [Column] LIKE '%(sr)%' OR [Column] LIKE '%(ii)%' OR [Column] LIKE '%(iii)%' OR [Column] LIKE '%(iv)%' Peter LarssonHelsingborg, Sweden |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-11-23 : 13:47:24
|
| i just wonder,what is replace inside a replace called?because it looks like derived table. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-23 : 14:02:41
|
| It's called effective writing.A derived table returns a resultset of records.Peter LarssonHelsingborg, Sweden |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-11-23 : 14:16:53
|
quote: Originally posted by funketekun i just wonder,what is replace inside a replace called?because it looks like derived table.
It's generally referred to as "nesting". I made mention of that in my second post on this thread.Be One with the OptimizerTG |
 |
|
|
|