Search code examples
androidsqliteandroid-sqlite

SQLite filter records by month/year


I used the dumpCursor() method to verify if the db contains the proper values
But when I run:

myDB.rawQuery("SELECT * FROM " + FeedEntry.TABLE_NAME +" WHERE YEAR(date) = 2015", null);

(the date column is defined as "datetime DEFAULT NULL")
I get an uncaught exception.

What am I doing wrong?


Solution

  • I answered a similar question yesterday: https://stackoverflow.com/a/33657233/2649012

    The key is using the strftime() function.
    The parameter to pass is '%m' for a monthly search or '%Y' for a yearly search.

    Therefore, change your query to

    myDB.rawQuery("SELECT * FROM " + FeedEntry.TABLE_NAME +" WHERE strfttime('%Y', date) = '2015'", null);