I found a strange behaviour when ordering a table where a row is defined with COLLATE NOCASE and the data contains empty strings.

Create a table like this:

CREATE TABLE test (col_a TEXT COLLATE NOCASE, col_b TEXT COLLATE NOCASE);
INSERT INTO test (col_a, col_b) VALUES ('a', '');
INSERT INTO test (col_a, col_b) VALUES ('a', 'a');
INSERT INTO test (col_a, col_b) VALUES ('a', 'b');
INSERT INTO test (col_a, col_b) VALUES ('b', '');
INSERT INTO test (col_a, col_b) VALUES ('b', 'a');
INSERT INTO test (col_a, col_b) VALUES ('b', 'b');

Now select the data using col_b and col_a

SELECT * from test ORDER BY col_b, col_a;

the expected result should be

a|
b|
a|a
b|a
a|b
b|b

but the result is

a|
a|a
b|a
b|
a|b
b|b

To obtain the correct result you must use this query:
SELECT * from test ORDER BY lower(col_b), lower(col_a);

It the table is defined without the "COLLATE NOCASE" definition, the sorting works ok with both the queries.

It is a feature or a bug?
I think the problem concerns the empty strings... maybe that when COLLATE NOCASE is defined, the comparison between an empty string and some data returns EQUAL.


Paolo

Reply via email to