Different behaviour in “order by” clause: Oracle vs. PostgreSQL

感情迁移 提交于 2019-12-18 08:57:11

问题


I have the following table (created and populated them in Oracle and PostgreSQL):

> create table foo (a varchar(10));

I populated them with values, and order by clause is behaving differently in PostgreSQL and Oracle (I don't think versions are relevant to this question):

Oracle:

> select a, length(a) from foo order by a;
A       LENGTH(A)
---------- ----------
.1          2
01          2
1           1
1#0         3
1#1         3
1.0         3
1.1         3
10          2
11          2

9 rows selected.

I get what I expect. .1 before 01, since . is before 0 in ascii table.

However, in PostgreSQL I have:

=> select a, length(a) from foo order by a;
  a  | length 
-----+--------
 01  |      2
 1   |      1
 .1  |      2
 10  |      2
 1.0 |      3
 1#0 |      3
 11  |      2
 1.1 |      3
 1#1 |      3
(9 rows)

Why the difference? I know it probably has something to do with collate order or similar, but I would like some pointers on where to read more about it.

UPDATE: collate info on the PostgreSQL database:

Encoding: UTF8
Collante: en_US.UTF-8
Ctype: en_US.UTF-8 | 

Thanks!


回答1:


Postgres has only two built-in collations: C and POSIX. Any other collations are provided by operating system. On many linux systems in UTF locales all non alphanumeric characters are ignored during sorting.

You can obtain expected result using collate C:

select a, length(a) from foo order by a collate "C";

You can find more detailed explanation in this answer.



来源:https://stackoverflow.com/questions/31528037/different-behaviour-in-order-by-clause-oracle-vs-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!