替换外来字符替换 'ss' 而不是 SQL 函数中的 'ß'
Replacing Foreign Chars replaces 'ss' instead of 'ß' in SQL Function
我自己写了一个快速函数来修复and/or替换外来字符。
出于某种原因,它将 'ss' 视为“ß”并错误地替换了它。到底是怎么回事?我如何确保只有“ß”被替换?
ALTER FUNCTION [dbo].[fn_ReplaceForeignChars](
@mode int,
@inStr nvarchar(max) )
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @outStr nvarchar(max)
SET @outStr = @inStr
SET @outStr = REPLACE(@outStr, 'ü', 'ü')
SET @outStr = REPLACE(@outStr, 'Ü', 'Ü')
SET @outStr = REPLACE(@outStr, 'ö', 'ö')
SET @outStr = REPLACE(@outStr, 'ß', 'ß')
SET @outStr = REPLACE(@outStr, 'é', 'é')
SET @outStr = REPLACE(@outStr, 'ø', 'ø')
SET @outStr = REPLACE(@outStr, 'è', 'é')
SET @outStr = REPLACE(@outStr, 'ë', 'ë')
SET @outStr = REPLACE(@outStr, 'ô', 'ô')
SET @outStr = REPLACE(@outStr, 'Ã…', 'Å')
SET @outStr = REPLACE(@outStr, 'ä', 'ä')
SET @outStr = REPLACE(@outStr, 'Ã¥', 'å')
SET @outStr = REPLACE(@outStr, 'ó', 'ó')
SET @outStr = REPLACE(@outStr, 'ú', 'ú')
SET @outStr = REPLACE(@outStr, 'á', 'á')
SET @outStr = REPLACE(@outStr, 'É', 'É')
SET @outStr = REPLACE(@outStr, 'Ö', 'Ö')
SET @outStr = REPLACE(@outStr, 'Ã', 'í')-- (Hidden char next to Ã)
SET @outStr = REPLACE(@outStr, 'ñ', 'ñ')
SET @outStr = REPLACE(@outStr, 'ò', 'ò')
SET @outStr = REPLACE(@outStr, 'Ä', 'Ä')
IF (@mode = 1)
BEGIN
SET @outStr = REPLACE(@outStr, 'ü', 'u')
SET @outStr = REPLACE(@outStr, 'ú', 'u')
SET @outStr = REPLACE(@outStr, 'Ü', 'U')
SET @outStr = REPLACE(@outStr, 'ö', 'o')
SET @outStr = REPLACE(@outStr, 'ô', 'o')
SET @outStr = REPLACE(@outStr, 'ó', 'o')
SET @outStr = REPLACE(@outStr, 'ò', 'o')
SET @outStr = REPLACE(@outStr, 'ø', 'o')
SET @outStr = REPLACE(@outStr, 'Ö', 'O')
SET @outStr = REPLACE(@outStr, 'ß', 'B')
SET @outStr = REPLACE(@outStr, 'é', 'e')
SET @outStr = REPLACE(@outStr, 'é', 'e')
SET @outStr = REPLACE(@outStr, 'ë', 'e')
SET @outStr = REPLACE(@outStr, 'É', 'E')
SET @outStr = REPLACE(@outStr, 'Å', 'A')
SET @outStr = REPLACE(@outStr, 'Ä', 'A')
SET @outStr = REPLACE(@outStr, 'ä', 'a')
SET @outStr = REPLACE(@outStr, 'å', 'a')
SET @outStr = REPLACE(@outStr, 'á', 'a')
SET @outStr = REPLACE(@outStr, 'í', 'i')
SET @outStr = REPLACE(@outStr, 'ñ', 'n')
END
RETURN @outStr
END
What is going on?
当前排序规则认为 'ss' 和 'ß' 是等价的。
您可以 运行 在具有二进制排序规则的数据库中,或在表达式中指定排序规则,如下所示:
DECLARE @outStr nvarchar(max) = 'floss'
SET @outStr = REPLACE(@outStr, 'ß' collate Latin1_General_100_BIN2, 'B')
select @outStr
我自己写了一个快速函数来修复and/or替换外来字符。
出于某种原因,它将 'ss' 视为“ß”并错误地替换了它。到底是怎么回事?我如何确保只有“ß”被替换?
ALTER FUNCTION [dbo].[fn_ReplaceForeignChars](
@mode int,
@inStr nvarchar(max) )
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @outStr nvarchar(max)
SET @outStr = @inStr
SET @outStr = REPLACE(@outStr, 'ü', 'ü')
SET @outStr = REPLACE(@outStr, 'Ü', 'Ü')
SET @outStr = REPLACE(@outStr, 'ö', 'ö')
SET @outStr = REPLACE(@outStr, 'ß', 'ß')
SET @outStr = REPLACE(@outStr, 'é', 'é')
SET @outStr = REPLACE(@outStr, 'ø', 'ø')
SET @outStr = REPLACE(@outStr, 'è', 'é')
SET @outStr = REPLACE(@outStr, 'ë', 'ë')
SET @outStr = REPLACE(@outStr, 'ô', 'ô')
SET @outStr = REPLACE(@outStr, 'Ã…', 'Å')
SET @outStr = REPLACE(@outStr, 'ä', 'ä')
SET @outStr = REPLACE(@outStr, 'Ã¥', 'å')
SET @outStr = REPLACE(@outStr, 'ó', 'ó')
SET @outStr = REPLACE(@outStr, 'ú', 'ú')
SET @outStr = REPLACE(@outStr, 'á', 'á')
SET @outStr = REPLACE(@outStr, 'É', 'É')
SET @outStr = REPLACE(@outStr, 'Ö', 'Ö')
SET @outStr = REPLACE(@outStr, 'Ã', 'í')-- (Hidden char next to Ã)
SET @outStr = REPLACE(@outStr, 'ñ', 'ñ')
SET @outStr = REPLACE(@outStr, 'ò', 'ò')
SET @outStr = REPLACE(@outStr, 'Ä', 'Ä')
IF (@mode = 1)
BEGIN
SET @outStr = REPLACE(@outStr, 'ü', 'u')
SET @outStr = REPLACE(@outStr, 'ú', 'u')
SET @outStr = REPLACE(@outStr, 'Ü', 'U')
SET @outStr = REPLACE(@outStr, 'ö', 'o')
SET @outStr = REPLACE(@outStr, 'ô', 'o')
SET @outStr = REPLACE(@outStr, 'ó', 'o')
SET @outStr = REPLACE(@outStr, 'ò', 'o')
SET @outStr = REPLACE(@outStr, 'ø', 'o')
SET @outStr = REPLACE(@outStr, 'Ö', 'O')
SET @outStr = REPLACE(@outStr, 'ß', 'B')
SET @outStr = REPLACE(@outStr, 'é', 'e')
SET @outStr = REPLACE(@outStr, 'é', 'e')
SET @outStr = REPLACE(@outStr, 'ë', 'e')
SET @outStr = REPLACE(@outStr, 'É', 'E')
SET @outStr = REPLACE(@outStr, 'Å', 'A')
SET @outStr = REPLACE(@outStr, 'Ä', 'A')
SET @outStr = REPLACE(@outStr, 'ä', 'a')
SET @outStr = REPLACE(@outStr, 'å', 'a')
SET @outStr = REPLACE(@outStr, 'á', 'a')
SET @outStr = REPLACE(@outStr, 'í', 'i')
SET @outStr = REPLACE(@outStr, 'ñ', 'n')
END
RETURN @outStr
END
What is going on?
当前排序规则认为 'ss' 和 'ß' 是等价的。
您可以 运行 在具有二进制排序规则的数据库中,或在表达式中指定排序规则,如下所示:
DECLARE @outStr nvarchar(max) = 'floss'
SET @outStr = REPLACE(@outStr, 'ß' collate Latin1_General_100_BIN2, 'B')
select @outStr