正如 Aaron 所说,我认为您不能完全在 SQL 中处理映射表,但是将字符映射到 ASCII-7 应该涉及一些相当简单的表,与 AI 排序规则一起使用。这里有两张表,一张用于映射列中的字符,一张用于字母表中的字母(如有必要,可以展开)。
通过使用 AI 排序规则,我得到了很多明确的映射定义。
-----------------------------------------------
-- One time mapping table setup
CREATE TABLE t4000(i INT PRIMARY KEY);
GO
INSERT INTO t4000 --Just a simple list of integers from 1 to 4000
SELECT ROW_NUMBER()OVER(ORDER BY a.x)
FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) a(x)
CROSS APPLY (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) b(x)
CROSS APPLY (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) c(x)
CROSS APPLY (VALUES(1),(2),(3),(4)) d(x)
GO
CREATE TABLE TargetChars(ch NVARCHAR(2) COLLATE Latin1_General_CS_AI PRIMARY KEY);
GO
INSERT TargetChars -- A-Z, a-z, ss
SELECT TOP(128) CHAR(i)
FROM t4000
WHERE i BETWEEN 65 AND 90
OR i BETWEEN 97 AND 122
UNION ALL
SELECT 'ss'
-- plus any other special targets here
GO
-----------------------------------------------
-- function
CREATE FUNCTION dbo.TrToA7(@str NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @mapped NVARCHAR(4000) = '';
SELECT TOP(LEN(@str))
@mapped += ISNULL(tc.ch, SUBSTRING(@str, i, 1))
FROM t4000
LEFT JOIN TargetChars tc ON tc.ch = SUBSTRING(@str, i, 1)
COLLATE Latin1_General_CS_AI;
RETURN @mapped;
END
GO
使用示例:
SELECT dbo.TrToA7('It was not á tötal löß.');
结果:
--------------------------
It was not a total loss.