Character mapping / search and replace character by character in SQL Server 2008 R2

前端 未结 1 1741
广开言路
广开言路 2021-01-28 19:28

I am running on SQL Server 2008 R2 and we have a requirement here whereby I need to create replace certain English characters to locale language characters used previously in le

相关标签:
1条回答
  • 2021-01-28 19:39

    Does this run any faster for you? (I used my collation of SQL_Latin1_General_CP1_CS_AS, you might want to change that).

    ALTER FUNCTION [dbo].[ArabicToString] (@inString VARCHAR(MAX))
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
        DECLARE @MappingCharacters TABLE
        (
            InputCharacter NCHAR(1) COLLATE SQL_Latin1_General_CP1_CS_AS PRIMARY KEY,
            OutputChar NCHAR(1)
        )
    
        INSERT @MappingCharacters
        VALUES
            ('A', 'ء')
            ,('B', 'آ')
            ,('C', 'أ')
            ,('D', 'ؤ')
            ,('E', 'إ')
            ,('F', 'ئ')
            ,('G', 'ا')
            ,('H', 'ب')
            ,('I', 'ة')
            ,('J', 'ت')
            ,('K', 'ث')
            ,('L', 'ج')
            ,('M', 'ح')
            ,('N', 'خ')
            ,('O', 'د')
            ,('P', 'ذ')
            ,('Q', 'ر')
            ,('R', 'ز')
            ,('S', 'س')
            ,('T', 'ش')
            ,('U', 'ص')
            ,('V', 'ض')
            ,('W', 'ط')
            ,('X', 'ظ')
            ,('Y', 'ع')
            ,('Z', 'غ')
            ,('a', 'ف')
            ,('b', 'ق')
            ,('c', 'ك')
            ,('d', 'ل')
            ,('e', 'م')
            ,('f', 'ن')
            ,('g', 'ه')
            ,('h', 'و')
            ,('i', 'ى')
            ,('j', 'ي')
            ,('v', 'ـ')
            ,('1', '١')
            ,('2', '٢')
            ,('3', '٣')
            ,('4', '٤')
            ,('5', '٥')
            ,('6', '٦')
            ,('7', '٧')
            ,('8', '٨')
            ,('9', '٩')
            ,('0', '٠')
            ,('/', '\')
    
        DECLARE @Result NVARCHAR(MAX) = ''
            , @Position INT = 1
            , @StrLength INT = DATALENGTH(@inString)
    
        DECLARE @Vchar char(1), @NextChar NCHAR(1)
    
        SET @VChar = SUBSTRING(@inString, @Position, 1)
        IF ASCII(@Vchar) > 189 and ASCII(@Vchar) < 255 
        RETURN Rtrim(Ltrim(@inString))
    
        WHILE (@Position <= @StrLength)  -- leave loop if bad character found
        BEGIN
            -- Reset holders
            SET @NextChar = SUBSTRING(@inString, @Position, 1)
    
            SET @Result = @Result + ISNULL((SELECT OutputChar FROM @MappingCharacters MC WHERE InputCharacter = @NextChar COLLATE SQL_Latin1_General_CP1_CS_AS), @NextChar)
    
            -- Add one to position 
            SET @Position= @Position + 1
        END
    
      RETURN Rtrim(Ltrim(@Result))
    END
    GO
    

    It is very similar, but I'm hoping that rather than a very large series of else if checks, SQL Server will perform better when working with a set. I'm not sure if this would work better if @MappingCharacters was a real table or not.

    0 讨论(0)
提交回复
热议问题