DayLaborer
Programmer
I need to remove all non-Ascii chars from a string. So I tried the function that I pasted down below, but it doesn't exactly work.
[tt]select dbo.fnRemoveNonLowAsciiCharacters('This character – is not really a hyphen')[/tt]
will return
'This character is not really a hyphen'
But if I run this:
[tt]select dbo.fnRemoveNonLowAsciiCharacters('?')[/tt]
it returns
'F'
How can I fix this function to truly strip-out all non-ascii characters?
Thanks!
Eliezer
[tt]select dbo.fnRemoveNonLowAsciiCharacters('This character – is not really a hyphen')[/tt]
will return
'This character is not really a hyphen'
But if I run this:
[tt]select dbo.fnRemoveNonLowAsciiCharacters('?')[/tt]
it returns
'F'
How can I fix this function to truly strip-out all non-ascii characters?
Code:
ALTER FUNCTION dbo.fnRemoveNonLowAsciiCharacters (@OldString as varChar(2000))
RETURNS varChar(2000) AS
BEGIN
DECLARE @Count integer, @NewString varChar(2000)
SET @Count =1
SET @NewString = ''
WHILE @Count <= Len(@OldString)
BEGIN
--If the character is not a regular ascii character it will be removed
IF ASCII(SUBSTRING(@OldString,@Count,1)) < 127
BEGIN
SET @NewString = @NewString + SUBSTRING(@OldString,@Count,1)
END
SET @Count = @Count + 1
END
RETURN @NewString
END
Eliezer