1 oracle postgresql database-migration
我在 Oracle 和 Postgres 中运行以下查询,两者在值的排序方面显示不同的输出。
with test as (
select 'Summary-Account by User (Using Contact ID)' col1 from dual
union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1 from dual
)
select * from test
order by col1 desc;
Run Code Online (Sandbox Code Playgroud)
下面是甲骨文的一个

Postgres
with test as (
select 'Summary-Account by User (Using Contact ID)' col1
union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1
)
select * from test
order by col1 desc;
Run Code Online (Sandbox Code Playgroud)

Oracle 排序规则是 AL32UTF8 Postgres 的 LC_CTYPS 是 en_US.UTF-8
从数据库的行为方式来看,它们看起来是相同的。如何解决这个问题?
在将查询 order by 更改为order by col1 collate "C" desc;后,我在 stackoverflow 上阅读了一些关于 POSIX 和 C 的帖子;结果与 Oracle 输出匹配。
有没有办法永久应用这个?
AL32UTF8不是排序规则,而是编码(字符集)。
Oracle 默认使用 \xe2\x80\x9c 二进制排序规则\xe2\x80\x9d,它对应于 PostgreSQL 中的 theC或POSIX排序规则。
您有多种选择可以在 PostgreSQL 中获得类似的结果:
\n创建数据库LOCALE "C"
如果您从表中进行选择,请定义要使用"C"排序规则的列:
ALTER TABLE tab ALTER col1 TYPE text COLLATE "C";\nRun Code Online (Sandbox Code Playgroud)\n添加一个明确的COLLATE子句:
ORDER BY col1 COLLATE "C"\nRun Code Online (Sandbox Code Playgroud)\n