如何从T-SQL的字符串中删除扩展的ASCII字符? [英] How do I remove extended ASCII characters from a string in T-SQL?

查看:66
本文介绍了如何从T-SQL的字符串中删除扩展的ASCII字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从T-SQL的SELECT语句中过滤(删除)扩展的ASCII字符.

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.

预期输入:

ËËËËeeeeËËËË

预期输出:

eeee

我发现的所有内容都是针对 MySQL 的.

All that I've found is for MySQL.

我正在使用:

Microsoft SQL Server Management Studio  11.0.2100.60
Microsoft .NET Framework    4.0.30319.17929

推荐答案

好的,尝试一下.看来他们有同样的问题.无论如何,您都需要根据自己的需求进行修改.

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

SqlServerCentral

这篇关于如何从T-SQL的字符串中删除扩展的ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆