Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181290

Different behaviour in "order by" clause: Oracle vs. PostgreSQL

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!

Upvotes: 3

Views: 5260

Answers (1)

klin
klin

Reputation: 121604

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.

Upvotes: 6

Related Questions