SQLite Query Alphabetical Case-insensitive

All I want to do is grab the material in alphabetical order and ignore the capital letters.

db.rawQuery("SELECT " + catName + " FROM "+tableName+" ORDER BY "+catName+" ASC COLLATE NOCASE;", null); 

This is the code that I use above, but it always gives me an SQLite exception saying that COLLATE is a syntax error.

android.database.sqlite.SQLiteException: next to "COLLATE": syntax error: when compiling: SELECT Artist FROM testTable COLLATE NOCASE ASC

+56
android sql sqlite sql-order-by collate
May 11 '11 at
source share
3 answers

COLLATE goes to the direction of the order:

 db.rawQuery("SELECT " + catName + " FROM " +tableName +" ORDER BY "+catName+" COLLATE NOCASE ASC;", null); 

But you do not need ASC - by default, so you can just use:

 db.rawQuery("SELECT "+ catName +" FROM "+ tableName +" ORDER BY "+ catName +" COLLATE NOCASE;", null); 
+149
May 11 '11 at 12:53 a.m.
source share

add COLLATE NOCASE after OrderBy line.

db.query (table, columns, selection, selectionArgs, groupBy, having, orderBy + "COLLATE NOCASE ASC");

here, the order from ASC or DESC depends on your needs.

+7
Apr 17 '15 at 15:21
source share

This should work too:

 db.rawQuery("SELECT "+ catName +" FROM "+ tableName +" ORDER BY lower("+ catName +");", null); 
+1
Mar 03 '15 at 20:22
source share



All Articles