小编典典

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

sql

我正在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

如果有人可以建议一种不同的方法,那么除了替换功能或我们目前使用的上述方法之外,我将不胜感激。


阅读 176

收藏
2021-04-15

共1个答案

小编典典

这会为您运行得更快吗?(我使用了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

这是非常相似的,但是我希望else ifSQL
Server而不是进行大量检查,而在处理集合时会表现得更好。我不确定如果@MappingCharacters是真正的桌子,是否可以更好地工作。

2021-04-15