0

I am using ascii conversion to remove all letters/ characters other than a-z, A-Z and 0-9 from a string. But when I am trying the same, I am getting accented letters. While giving CustomerName with 'āb' its converted to 'ab' But my expected result is just 'b'. As per my understanding accented values are taking ascii value same as their base. Please help if you have any idea. Attaching my code

CREATE FUNCTION dbo.RemoveSpecialChar (@s VARCHAR(256)) 
RETURNS VARCHAR(256)
WITH SCHEMABINDING
    BEGIN
        IF @s IS NULL
            RETURN NULL
        DECLARE @s2 VARCHAR(256) = '',
                @l INT = LEN(@s),
                @p INT = 1

        WHILE @p <= @l
            BEGIN
                DECLARE @c INT
                SET @c = ASCII(SUBSTRING(@s, @p, 1))
                IF @c BETWEEN 48 AND 57
                   OR  @c BETWEEN 65 AND 90
                   OR  @c BETWEEN 97 AND 122
                    SET @s2 = @s2 + CHAR(@c)
                SET @p = @p + 1
            END

        IF LEN(@s2) = 0
            RETURN NULL

        RETURN @s2
        end

SELECT  [dbo].[RemoveSpecialChar](CAST(Name AS nvarchar(255))) as CustomerName
from  Customers
4

0 回答 0