sql - PostgreSQL upper function on the ascii 152 character ("ÿ")

标签 sql postgresql unicode

在 Windows 7 平台上,使用 PostgreSQL 版本 9.3.9,使用 PgAdmin 作为客户端,在包含例如“ÿÿÿ”返回 null。如果存储了三个值,例如

"ada"
"john"
"mole" 
"ÿÿÿ"

除了包含 "ÿÿÿ" 的行外,它们都以大写形式返回;这一排 什么都不给,null...

数据库编码方案是UTF8/UNICODE。设置“client_encoding”具有相同的值,UNICODE

这是数据库中的设置问题、操作系统问题还是错误 在数据库中?有一些推荐的解决方法吗?

结果:

select thecol, upper(thecol), upper(thecol) is null, convert_to(thecol, 'UTF8'), current_setting('server_encoding') from thetable where ... 

是:

"Apps";"APPS";f;"Apps";"UTF8"
"All";"ALL";f;"All";"UTF8"
"Test";"TEST";f;"Test";"UTF8"
"ÿÿÿ";"";f;"\303\277\303\277\303\277";"UTF8"

pg_settings 的 lc_ 部分是:

"lc_collate";"Swedish_Sweden.1252";"Shows the collation order locale."
"lc_ctype";"Swedish_Sweden.1252";"Shows the character classification and case conversion locale."
"lc_messages";"Swedish_Sweden.1252";"Sets the language in which messages are displayed."
"lc_monetary";"Swedish_Sweden.1252";"Sets the locale for formatting monetary amounts."
"lc_numeric";"Swedish_Sweden.1252";"Sets the locale for formatting numbers."

select * from pg_database 的输出是:

"template1";10;6;"Swedish_Sweden.1252";"Swedish_Sweden.1252";t;t;-1;12130;668;1‌​;1663;"{=c/postgres,postgres=CTc/postgres}" 
"template0";10;6;"Swedish_Sweden.1252";"Swedish_Sweden.1252";t;f;-1;12130;668;1‌​;1663;"{=c/postgres,postgres=CTc/postgres}"
"postgres";10;6;"Swedish_Sweden.1252";"Swedish_Sweden.1252";f;t;-1;12130;668;1;‌​1663;""

9.4.4版本的实际创建数据库语句是:

CREATE DATABASE postgres
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'Swedish_Sweden.1252'
       LC_CTYPE = 'Swedish_Sweden.1252'
       CONNECTION LIMIT = -1;

最佳答案

我的猜测是 upper 函数使用了数据库的 LC_CTYPE 设置。带分音符的拉丁文小写字母 Y (U+00FF) 的大写是带分音符的拉丁文大写字母 Y' (U+0178),它不是 Windows 1252 代码页的一部分。

如果您先将字符串转换为 Unicode 格式,upper 函数可能会按预期工作:

SELECT upper(convert_to(thecol, 'UTF8')) ...

您可能应该为 LC_CTYPELC_COLLATE 使用不同的值。在 Linux 上,您将使用 sv_SE.UTF-8

尽管如此,我认为这是 Postgres 中的一个错误。如果无法在目标字符集中表示大写版本,最好保留 ÿ

关于sql - PostgreSQL upper function on the ascii 152 character ("ÿ"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32690370/

相关文章:

sql - 如何在 postgresql 中获取第二个最大日期

SQL:选择该字母出现特定次数的列的第一个字母

mysql - 为什么mysql在两个事务同时更新时不以可重复读模式锁定一行?

linux - Haskell:quoteFile 在 unicode 字符上带有 "invalid byte sequence"的文本文件上失败

sql - 在 SQL Server 中的文本字段上使用 LEFT

sql - 更新+使用(行锁)+ CTE

python - django.db.utils.ProgrammingError : relation "auth_user" does not exist

ruby-on-rails - 运行 rake db :seed multiple times without creating duplicate records?

python unicode在用作字符串时而不是在打印时转换为原始文本字符

unicode - U+25BE的 "up-pointing"版本是什么?