【问题标题】:How does the SQL length function handle unicode graphemes?SQL 长度函数如何处理 unicode 字素?
【发布时间】:2018-01-13 01:50:52
【问题描述】:

考虑以下场景,其中我有由\U00000045\U00000301 定义的字符串

1) https://www.fileformat.info/info/unicode/char/0045/index.htm
2)https://www.fileformat.info/info/unicode/char/0301/index.htm

varchar(1) 约束的表是否会将其视为有效的1 个字符输入。还是会因为它被认为是 2 个字符的输入而被拒绝?

SQL一般如何处理带有字素的字符串的长度?

【问题讨论】:

  • @user2864740 该帖子没有谈论字素。只是 unicode 代码点。因为字素是多个 unicode 代码点,在语义上需要解释为单个图形 unicode 字符 - 我的问题仍未得到解答。
  • 它有演示如何回答问题的代码。它还提到了不同的数据库字符编码,这可能与上下文相关。

标签: sql postgresql unicode grapheme


【解决方案1】:

这个查询我可能看起来很傻,但仍然:

t=# with c(u) as (values( e'\U00000045\U00000301'))
select u, u::varchar(1), u::varchar(2),char_length(u), octet_length(u) from c;
 u | u | u | char_length | octet_length
---+---+---+-------------+--------------
 É | E | É |           2 |            3
(1 row)

编辑

t=# show server_encoding ;
 server_encoding
-----------------
 UTF8
(1 row)

t=# \l+ t
                                        List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges | Size  | Tablespace | Description
------+-------+----------+---------+-------+-------------------+-------+------------+-------------
 t    | vao   | UTF8     | C       | UTF-8 |                   | 51 MB | pg_default |
(1 row)

【讨论】:

  • 您能否也发布您测试过的数据库的字符集/编码/排序/ctype?
猜你喜欢
  • 2011-11-20
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2013-04-03
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
相关资源
最近更新 更多