I am using a function that I found here and else where on the internet to try and strip illegal characters from a field.
Create Function [epacube].[StripSpecs](@myString varchar(500),
@invalidChars varchar(100)) RETURNS varchar(500) AS Begin
While PatIndex(@invalidChars, @myString) > 0
Set @myString = Stuff(@myString, PatIndex(@invalidChars, @myString), 1, '')
Return @myString End
in my table I have set my field value to be: set DATA_NAME = ‘Pro$d)uc^t’
If I run this query:
SELECT epacube.StripSpecs (
DATA_NAME
,'%$%') FROM TABLE_DATA
It works and I get a value returned of Prod)uc^t
However, if I add another special character, it no longer works:
SELECT epacube.StripSpecs (
DATA_NAME
,'%$)%') FROM TABLE_DATA
returns my original value Pro$d)uc^t
Does anyone have any suggestion for accomplishing what I need to do?
EDIT
As per the answer below here is the code that worked:
Create Function [epacube].[StripSpecs](@myString varchar(500), @invalidChars varchar(100))
RETURNS varchar(500) AS
Begin
While PatIndex('%[' + @invalidChars + ']%', @myString) > 0
Set @myString = Stuff(@myString, PatIndex('%[' + @invalidChars + ']%', @myString), 1, '')
Return @myString
End
As with
LIKE, if you want to specify that one of a set of characters should match, use[]to enclose the set.Although, personally, given the descriptions of the function and parameters, I’d add the
%[and]%inStripSpecs, and let the caller just give a list of characters (if you don’t want to support any other type of pattern being specified)