SQLite Query in non case sensitive alphabetical order

AndroidSqlSqliteSql Order-ByCollate

Android Problem Overview


All I want to do is grab the stuff 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 I'm using above, but it always gives me an SQLite exception saying that COLLATE is a syntax error.

>android.database.sqlite.SQLiteException: near "COLLATE": syntax error: , while compiling: SELECT Artist FROM testTable COLLATE NOCASE ASC

Android Solutions


Solution 1 - Android

COLLATE goes before the order direction:

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

But you don't need the ASC -- that's the default so you could just as well use:

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

Solution 2 - Android

add COLLATE NOCASE after orderBy String.

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

here, order by ASC or DESC depends on your need.

Solution 3 - Android

This should work too I think:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnthony HoncianoView Question on Stackoverflow
Solution 1 - AndroidOMG PoniesView Answer on Stackoverflow
Solution 2 - AndroidminhazurView Answer on Stackoverflow
Solution 3 - AndroiddudemanView Answer on Stackoverflow