我正在尝试在 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'بسم اللہ الرحمن الرحیم');
注释:
- 此解决方案可能会导致性能下降
- 我在该函数中使用的字符范围尚未经过彻底测试。
- 有关阿拉伯 Unicode 字符集的完整引用,请参阅此文档 http://www.unicode.org/charts/PDF/U0600.pdf
关于sql - 执行字符串比较,忽略变音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23711832/