comaparison忽略了变音符号执行字符串

问题描述 投票:4回答:2

我在SQL Server阿拉伯文字试图搜索和需要忽略阿拉伯语变音符号。所以我使用Arabic_100_CI_AI整理。但它不能正常工作。

例如,对于下面的查询,我必须得1,但它没有结果!

select 1 
 where (N'مُحَمَّد'  Collate Arabic_100_CI_AI) = (N'محمّد' Collate Arabic_100_CI_AI)

这是什么问题,我如何能进行变音符号阿拉伯语文本不区分大小写比较?

sql sql-server arabic collation
2个回答
3
投票

这似乎AI标志没有阿拉伯语的工作。你可以建立自己的Unicode规范化功能。

ALTER FUNCTION [dbo].[NormalizeUnicode] 
(
    -- Add the parameters for the function here
    @unicodeWord nvarchar(max)
)
RETURNS nvarchar(max)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @Result nvarchar(max)

    -- Add the T-SQL statements to compute the return value here    
    declare @l int;
    declare @i int;

    SET @l = len(@unicodeWord + '-') - 1
    SET @i = 1;
    SET @Result = '';
    WHILE (@i <= @l)
    BEGIN
        DECLARE @c nvarchar(1);
        SET @c = SUBSTRING(@unicodeWord, @i, 1);
        -- 0x064B to 0x65F, 0x0670 are Combining Characters
        -- You may need to perform tests for this character range
        IF NOT (unicode(@c) BETWEEN 0x064B AND 0x065F or unicode(@c) = 0x0670)
            SET @Result = @Result + @c;
        SET @i = @i + 1;
    END

    -- Return the result of the function
    RETURN @Result
END

下面的测试应能正常工作,

select  1
where   dbo.NormalizeUnicode(N'بِسمِ اللہِ الرَّحمٰنِ الرَّحیم') = dbo.NormalizeUnicode(N'بسم اللہ الرحمن الرحیم');

笔记:

  1. 您可能会遇到性能下降与此解决方案
  2. 我在函数中使用的字符范围不彻底的测试。
  3. 有关阿拉伯Unicode字符集的完整参考,请参阅本文http://www.unicode.org/charts/PDF/U0600.pdf

1
投票

您使用排序规则是正确的,但如果你仔细看看在查询这两个阿拉伯单词(高亮粗体),它们是完全不同的,即使它们的意义相同,因此你没有得到的结果(因为比较不及格)

N“穆罕默德”和N“穆罕默德”

我敢肯定,如果你尝试使用unicode()功能,找出自己的Unicode值;他们的结果会有所不同。

如果您尝试以下查询,它会成功

select 1 
 where N'مُحَمَّد'  Collate Arabic_100_CI_AI like '%%'

看到这个帖子了一个更好的解释Treating certain Arabic characters as identical

© www.soinside.com 2019 - 2024. All rights reserved.