sql - 执行字符串比较,忽略变音符号

标签 sql sql-server arabic collation

我正在尝试在 SQL Server 中搜索阿拉伯文本,并且需要忽略阿拉伯变音符号。 所以我使用 Arabic_100_CI_AI 排序规则。但这行不通。

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

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

问题是什么?如何在阿拉伯文本中执行变音符号不敏感比较

最佳答案

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

关于sql - 执行字符串比较,忽略变音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711832/

相关文章:

sql-server - 全文搜索与阿拉伯语 Kaaf 字母在阿拉伯语模式下不匹配

mysql - 按位分组并选择不同值时 MyISAM 出现意外结果

SQL 舍入百分比使总和 100% - 1/3 为 0.34、0.33、0.33

mysql - 如何为没有内容的行打印零?

sql - 业务规则使用检查约束好不好

sql - 如何将一列的结果放入字符串变量中?

css - 带有阿拉伯语的多语言 UTF-8 网站

sql - Oracle SELECT - 一列的别名作为另一列的输入

sql - Hive描述扩展表返回json

php - 在 mysql 中使用变音符号搜索阿拉伯语