on 2010 Oct 25 3:20 PM
When importing data I need to remove a set of characters from a string to allow comparison with a column. The database is 9.0.2, so regular expressions are not available (at least to my knowledge).
Example:
String is 'A.628.164-09/11'
Remove all occurrences of '.,;-/'
Result 'A6281640911'
My first approach was to loop over all characters of the original string, using CHARINDEX to determine if it is to be skipped.
But I wonder, if there's a better solution. Any ideas?
Request clarification before answering.
Reimer, a similar question can be found here (more in the question than in answers), as an example of using several nested REPLACE() calls, like Thomas has suggested.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've used nested REPLACE-calls in some cases. For this one the string with chars to cut out should be a configurable string. So some kind of loop seems unavoidable to me. But I could try looping over the string with skip-chars calling REPLACE for each single-char substring and measure, which version well be faster with the customers data.
What about the replace() function ? If you have only a small number of characters to remove you can use a few hard coded calls which should be faster as a loop.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's the way I would do this, too, when there are not too many characters - usually as a chain of REPLACE() calls. And for performance reasons, I would think about using a LIKE '%[..]%' comparison beforehand to find out if any unwanted character exists in the string.
User | Count |
---|---|
41 | |
15 | |
10 | |
9 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.