字符映射/搜索和替换SQL Server 2008 R2中的字符

时间:2012-04-09 08:19:09

标签: sql sql-server sql-server-2008

我在SQL Server 2008 R2上运行,我们在这里有一个要求,我需要创建将某些英文字符替换为以前在旧系统中使用的区域设置语言字符。

为此,我可能会在T-SQL中使用replace函数,但在我的实践中,我们正逐字符替换

'ASkjDe'
A=char(XX)
S=char(XX)
k=char(XX)
j=char(XX)
D=char(XX)
e=char(XX)

现在,我想避免使用嵌套替换,这意味着我必须进行32个字符检查。是否有这样的功能,我可以传递它的值列表?

我们已经有一个功能,它可以进行替换,但速度非常慢,我们希望替换功能可以做得更好。以下是我们正在使用的当前功能

CREATE FUNCTION [dbo].[ArabicToString] (@inString VARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
  DECLARE @Vchar char(1), @Result NVARCHAR(MAX), @Flag BIT, @Position INT, @StrLength INT, @LChar CHAR, @HChar NCHAR

  SET @Result = ''
  SET @Position = 1
  SET @StrLength = DATALENGTH(@inString)  -- string length not going to change
  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 @LChar = SUBSTRING(@inString, @Position, 1)
      Set @HChar = ''

      if cast(@LChar as varbinary(1))=cast('*' as varbinary(1))
            Set @HChar = '*'
      else if cast(@LChar as varbinary(1)) = cast('-' as varbinary(1))
            set @HChar = '-'
      else if cast(@LChar as varbinary(1)) = cast('A' as varbinary(1))
            set @HChar = 'ء'
      else if cast(@LChar as varbinary(1)) = cast('B' as varbinary(1))
            set @HChar='آ'
      else if cast(@LChar as varbinary(1)) = cast('C' as varbinary(1))
            set @HChar='أ'
      else if cast(@LChar as varbinary(1)) = cast('D' as varbinary(1))
            set @HChar='ؤ'
      else if cast(@LChar as varbinary(1)) = cast('E' as varbinary(1))
            set @HChar='إ'
      else if cast(@LChar as varbinary(1)) = cast('F' as varbinary(1))
            set @HChar ='ئ'
      else if cast(@LChar as varbinary(1)) = cast('G' as varbinary(1))
            set @HChar='ا'
      else if cast(@LChar as varbinary(1)) = cast('H' as varbinary(1))
            set @HChar='ب'
      else if cast(@LChar as varbinary(1)) = cast('I' as varbinary(1))
            set @HChar='ة'
      else if cast(@LChar as varbinary(1)) = cast('J' as varbinary(1))
            set @HChar='ت'
      else if cast(@LChar as varbinary(1)) = cast('K' as varbinary(1))
            set @HChar='ث'
      else if cast(@LChar as varbinary(1)) = cast('L' as varbinary(1))
            set @HChar='ج'
      else if cast(@LChar as varbinary(1)) = cast('M' as varbinary(1))
            set @HChar='ح'
      else if cast(@LChar as varbinary(1)) = cast('N' as varbinary(1))
            set @HChar='خ'
      else if cast(@LChar as varbinary(1)) = cast('O' as varbinary(1))
            set @HChar='د'
      else if cast(@LChar as varbinary(1)) = cast('P' as varbinary(1))
            set @HChar='ذ'
      else if cast(@LChar as varbinary(1)) = cast('Q' as varbinary(1))
            set @HChar='ر'
      else if cast(@LChar as varbinary(1)) = cast('R' as varbinary(1))
            set @HChar='ز'
      else if cast(@LChar as varbinary(1)) = cast('S' as varbinary(1))
            set @HChar='س'
      else if cast(@LChar as varbinary(1)) = cast('T' as varbinary(1))
            set @HChar='ش'
      else if cast(@LChar as varbinary(1)) = cast('U' as varbinary(1))
            set @HChar='ص'
      else if cast(@LChar as varbinary(1)) = cast('V' as varbinary(1))
            set @HChar='ض'
      else if cast(@LChar as varbinary(1)) = cast('W' as varbinary(1))
            set @HChar='ط'
      else if cast(@LChar as varbinary(1)) = cast('X' as varbinary(1))
            set @HChar='ظ'
      else if cast(@LChar as varbinary(1)) = cast('Y' as varbinary(1))
            set @HChar='ع'
      else if cast(@LChar as varbinary(1)) = cast('Z' as varbinary(1))
            set @HChar='غ'
      else if cast(@LChar as varbinary(1)) = cast('a' as varbinary(1))
            set @HChar='ف'
      else if cast(@LChar as varbinary(1)) = cast('b' as varbinary(1))
            set @HChar='ق'
      else if cast(@LChar as varbinary(1)) = cast('c' as varbinary(1))
            set @HChar='ك'
      else if cast(@LChar as varbinary(1)) = cast('d' as varbinary(1))
            set @HChar='ل'    
      else if cast(@LChar as varbinary(1)) = cast('e' as varbinary(1))
            set @HChar='م'
      else if cast(@LChar as varbinary(1)) = cast('f' as varbinary(1))
            set @HChar='ن'
      else if cast(@LChar as varbinary(1)) = cast('g' as varbinary(1))
            set @HChar='ه'    
      else if cast(@LChar as varbinary(1)) = cast('h' as varbinary(1))
            set @HChar='و'
      else if cast(@LChar as varbinary(1)) = cast('i' as varbinary(1))
            set @HChar='ى'
      else if cast(@LChar as varbinary(1)) = cast('j' as varbinary(1))
            set @HChar='ي'    


      else if cast(@LChar as varbinary(1)) = cast('v' as varbinary(1))
            set @HChar='ـ'


      else if cast(@LChar as varbinary(1)) = cast('1' as varbinary(1))
            set @HChar='١'
      else if cast(@LChar as varbinary(1)) = cast('2' as varbinary(1))
            set @HChar='٢'    
      else if cast(@LChar as varbinary(1)) = cast('3' as varbinary(1))
            set @HChar='٣'
      else if cast(@LChar as varbinary(1)) = cast('4' as varbinary(1))
            set @HChar='٤'
      else if cast(@LChar as varbinary(1)) = cast('5' as varbinary(1))
            set @HChar='٥'
      else if cast(@LChar as varbinary(1)) = cast('6' as varbinary(1))
            set @HChar='٦'    
      else if cast(@LChar as varbinary(1)) = cast('7' as varbinary(1))
            set @HChar='٧'
      else if cast(@LChar as varbinary(1)) = cast('8' as varbinary(1))
            set @HChar='٨'
      else if cast(@LChar as varbinary(1)) = cast('9' as varbinary(1))
            set @HChar='٩'    
      else if cast(@LChar as varbinary(1)) = cast('0' as varbinary(1))
            set @HChar='٠'

      else if @LChar='/'
            set @HChar='\'

      select @Result=@Result + @HChar

      -- Add one to position 
      SET @Position= @Position + 1
  END

  RETURN Rtrim(Ltrim(@Result))
END

如果有人可以提出不同的方法,除了替换功能或我们目前使用的上述方法之外,我会很感激。

1 个答案:

答案 0 :(得分:2)

这对你来说运行得更快吗? (我使用了SQL_Latin1_General_CP1_CS_AS的排序规则,您可能想要更改它。)

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

它非常相似,但我希望SQL Server在使用集合时能够表现得更好,而不是一系列非常大的else if检查。我不确定如果@MappingCharacters是真实的桌子,这是否会更好。