sql-server - 未排序的 Unicode (UTF-16) 数据如何存储在 varchar 列中?

标签 sql-server unicode encoding collation varchar

这纯粹是理论问题,需要我思考

假设我有 Unicode cyclone (🌀 1F300) 符号。如果我尝试将它存储在具有默认 Latin1_General_CI_AS 排序规则的 varchar 列中,旋风符号不能不适合 varchar 中每个符号使用的一个字节>...

我可以看到的方式:

  1. 就像 javascript 对基本平面 ( BMP ) 之外的符号所做的那样,它将它们存储为 2 个符号(代理对),然后需要对 put them back together 进行额外处理...
  2. 只需截断符号,存储第一个字节并删除第二个字节....(数据是 toast - 你应该阅读手册....)
  3. 数据被销毁,没有任何用处被保存...(数据完蛋了——你应该读过手册....)
  4. 超出我心智能力的其他一些选择.....

插入几个不同的 unicode 符号后我做了一些研究

 INSERT INTO [Table] (Field1)
 VALUES ('👽')

 INSERT INTO [Table] (Field1)
 VALUES ('🌀')

然后将它们作为字节读取 SELECT cast (field1 as varbinary(10)) 在这两种情况下我都得到了 0x3F3F

enter image description here

ascii 中的

3F? ( question mark ) 例如两个问号 (??)我还看到,在执行正常的 select * 时,这是否意味着数据是 toast ,甚至没有存储第一口?

未排序的 Unicode 数据如何存储在 varchar 列中?

最佳答案

数据是 toast,正是你所看到的,2 x 0x3F 字节。这发生在插入之前的类型转换期间,并且实际上与 cast('👽' as varbinary(2)) 相同,后者也是 0xF3F3(与转换 N'👽' 相反)。

When Unicode data must be inserted into non-Unicode columns, the columns are internally converted from Unicode by using the WideCharToMultiByte API and the code page associated with the collation. If a character cannot be represented on the given code page, the character is replaced by a question mark (?) Ref.

关于sql-server - 未排序的 Unicode (UTF-16) 数据如何存储在 varchar 列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099862/

相关文章:

c++ - 打印 unicode 字符

Java编码: why the output is always the same?

sql-server - 使用通配符的 T-SQL xquery .modify 方法

java - 参数未传递给过程

sql-server - 在动态加载的数据中缺少日期时在 ZingChart 中制作图表间隙?

paypal支付中编码金额

php - PHP 中函数 md5() 的(默认)编码是什么?

sql-server - 从 SQL Server 2008 Standard 升级到 Developer?

java - 为什么 Java 允许在其标识符中使用控制字符?

python - 如何使用 Python 反转 Unicode 分解?