Theres something I don't understand with regards to a CASE
statement in a ORDER BY
clause using SQLite (initially I was using it with Android Room but its not specific to that).
This is a distillation of the problem. Query 1 returns what I was expecting (a case insensitive, ordered list of the stuff). Whereas Query 2 appears to ignore the column's COLLATE NOCASE. Why is that?
Thanks!
Schema (SQLite v3.30)
CREATE TABLE "test" (
"id" INTEGER,
"stuff" TEXT COLLATE NOCASE,
PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO test (stuff) VALUES ("0");
INSERT INTO test (stuff) VALUES ("a");
INSERT INTO test (stuff) VALUES ("z");
INSERT INTO test (stuff) VALUES ("A");
INSERT INTO test (stuff) VALUES ("Z");
Query #1
SELECT *
FROM test
ORDER BY
stuff ASC;
id | stuff |
---|---|
1 | 0 |
2 | a |
4 | A |
3 | z |
5 | Z |
Query #2
SELECT *
FROM test
ORDER BY
CASE WHEN true THEN stuff END ASC;
id | stuff |
---|---|
1 | 0 |
4 | A |
5 | Z |
2 | a |
3 | z |