0
insert into @res
    select * from (
        select * from myTable where my_name like '%'+@token+'%'
    union

        --replace special characters
        select * from myTable where my_name like '%'+REPLACE ( @token , 'e' , 'è' )+'%' order by name
        select * from myTable where my_name like '%'+REPLACE ( @token , 'e' , 'é' )+'%' order by name
        select * from myTable where my_name like '%'+REPLACE ( @token , 'e' , 'Ě' )+'%' order by name
        ----

) as tmp order by name

注释之间的代码部分比这里长得多。

如何在不丢失其功能的情况下将其移至新功能?

4

1 回答 1

1

您可以使用LIKE这样的字符串%ar[e,è,é,Ě]na%,它将覆盖您的所有工会。如果规则太多,这里有一个函数可以做到这一点:

干得好

CREATE FUNCTION transform (@inStr varchar(300))
returns varchar(255)
AS  
BEGIN
DECLARE @registry varchar(300),
    @curChar varchar(20),
    @outStr varchar(300),
    @counter int,
    @start int,
    @end int;

SET @outStr = '%';
/* Creating a registry of replacements in format {X}[x,X,Xx];
   Where {X} contains the character to be replaced,
   [x,X,Xx]; contains the replacemet characters
*/
SET @registry = '{e}[e,è,é,Ě];
                 {s}[ ..other translations of "s" go here.. ];
                 {n}[n,N];';

set @counter = 1;

WHILE (LEN(@inStr) >= @counter)
    BEGIN
        SELECT @curChar = substring(@inStr, @counter, 1)
        IF (CHARINDEX( '{' + @curChar + '}', @registry, 1) > 0)
            BEGIN
                SELECT @start = CHARINDEX( '{' + @curChar + '}', @registry, 1) + 2;
                SELECT @end = CHARINDEX( ';', @registry, @start);
                SELECT @curChar = substring(@registry, @start + 1, @end - @start - 1);
            END

        SET @outStr = @outStr + @curChar
        SET @counter = @counter + 1;
    END 
SET @outStr = @outStr + '%'

RETURN @outStr; 
END

例如这里

... WHERE x.str like transform('arena')

该函数将返回%ar[e,è,é,Ě][n,N]a%。此字符串表示 - 任何包含以 开头的字符串的值ar,下一个字符是任何一个e,è,é,Ě,下一个字符是任何一个n,N并以 结尾a

所以...

    select * from myTable where my_name like transform(@token)

将涵盖任何变化,您将不再需要这些工会。

于 2013-04-01T10:35:35.640 回答