这纯粹是理论问题,需要我思考
假设我有 Unicode
cyclone (🌀 1F300
) 符号。如果我尝试将它存储在具有默认 Latin1_General_CI_AS
排序规则的 varchar
列中,旋风符号不能不适合 varchar
中每个符号使用的一个字节>...
我可以看到的方式:
- 就像 javascript 对基本平面 ( BMP ) 之外的符号所做的那样,它将它们存储为 2 个符号(代理对),然后需要对 put them back together 进行额外处理...
- 只需截断符号,存储第一个字节并删除第二个字节....(数据是 toast - 你应该阅读手册....)
- 数据被销毁,没有任何用处被保存...(数据完蛋了——你应该读过手册....)
- 超出我心智能力的其他一些选择.....
插入几个不同的 unicode 符号后我做了一些研究
INSERT INTO [Table] (Field1)
VALUES ('👽')
INSERT INTO [Table] (Field1)
VALUES ('🌀')
然后将它们作为字节读取 SELECT
cast (field1 as varbinary(10))
在这两种情况下我都得到了 0x3F3F
。
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/