根据 MySQL 返回D8B3D984D8A7D985
的SELECT HEX(CAST('سلام' as binary))
结果,MySQL 似乎正在使用 UTF-8 字符编码。
在 SQL Server 2019 上,您可以使用 LATIN1_GENERAL_100_CI_AS_SC_UTF8 排序规则(SQL Server 2017 不支持),如下所示:
create table #Test (
UTF16 nvarchar(max),
UTF8 varchar(max) COLLATE LATIN1_GENERAL_100_CI_AS_SC_UTF8
)
insert #Test values (N'سلام', N'سلام');
select UTF16 from #Test;
select CAST(UTF16 as varbinary) as [UTF16-Bytes] from #Test;
select UTF8 from #Test;
select CAST(UTF8 as varbinary) as [UTF8-Bytes] from #Test;
返回:
UTF16
سلام
UTF16-Bytes
0x3306440627064506
UTF8
سلام
UTF8-Bytes
0xD8B3D984D8A7D985
然后使用 hashbytes():
select hashbytes('MD5', cast(UTF16 as varbinary)) as [UTF16-Hash] from #Test;
select hashbytes('MD5', cast(UTF8 as varbinary)) as [UTF8-Hash] from #Test;
返回:
UTF16-Hash
0x0381CA5081FBC68B2F55F2F2C21399D7
UTF8-Hash
0x78903C575B0DDA53C4A7644A2DD36D0E
希望这可以帮助!