I'm trying to write a function that strips out all non-ASCII characters in a field when I select it, but I'm having trouble with the pattern matching. I've tried this (using the collation statement to remove the diacritics):
However, when I test it using this:
It returns this: arstlkjDSFGHSDFGEhq4e5987nq345vui£345v
As you can see, that still allows the character to come through despite the fact that its ASCII code is 146. Conversely, it removes something like ; which has an ASCII code of 59.
Short of individually listing each character that's allowed, how can I get it to stick to the range I've asked for?
Thanks...
Code:
Create Function dbo.udfRemoveTroublesomeCharacters (@IncomingText VarChar(1000))
Returns VarChar(1000)
As
Begin
Select @IncomingText = @IncomingText Collate SQL_Latin1_General_CP1253_CI_AI
Declare @KeepValues As VarChar(50)
Set @KeepValues = '%[' + CHAR(32) + '-' + CHAR(126) + ']%'
While PatIndex(@KeepValues, @IncomingText) > 0
Set @IncomingText = Stuff(@IncomingText, PatIndex(@KeepValues, @IncomingText), 1, '')
Return @IncomingText
End
Go
Code:
Select dbo.udfRemoveTroublesomeCharacters('ars;tlkjDSFGHSDFGÉ hq4é5987nq34[5vui$"£%^"$%^"(*345v')
As you can see, that still allows the character to come through despite the fact that its ASCII code is 146. Conversely, it removes something like ; which has an ASCII code of 59.
Short of individually listing each character that's allowed, how can I get it to stick to the range I've asked for?
Thanks...