How do I remove extended ASCII characters from a string in T-SQL?

前端 未结 5 645
忘了有多久
忘了有多久 2020-11-30 06:00

I need to filter out (remove) extended ASCII characters from a SELECT statement in T-SQL.

I\'m using a stored procedure to do so.

Expected input:

<         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 06:44

    OK, give this a try. It seems the same issue they have. Anyway you need to modify it based on your requirements.

    CREATE FUNCTION RemoveNonASCII 
    (
        @nstring nvarchar(255)
    )
    RETURNS varchar(255)
    AS
    BEGIN
    
        DECLARE @Result varchar(255)
        SET @Result = ''
    
        DECLARE @nchar nvarchar(1)
        DECLARE @position int
    
        SET @position = 1
        WHILE @position <= LEN(@nstring)
        BEGIN
            SET @nchar = SUBSTRING(@nstring, @position, 1)
            --Unicode & ASCII are the same from 1 to 255.
            --Only Unicode goes beyond 255
            --0 to 31 are non-printable characters
            IF UNICODE(@nchar) between 32 and 255
                SET @Result = @Result + @nchar
            SET @position = @position + 1
        END
    
        RETURN @Result
    
    END
    GO
    

    Check it out at SqlServerCentral

提交回复
热议问题