首页 > 解决方案 > Querying the Month part of a date in SQLite

问题描述

I can query year part of a date using String [] selectionArgs = { "2018" };

String query =
  "SELECT * FROM TABLE" +
  "WHERE strftime('%Y', COLUMN_DATE) = ?" + 
  "ORDER BY strftime('%m', COLUMN_DATE) DESC;"

return db.rawQuery(query, selectionArgs);

But I am having trouble querying the month part of the date like I did for the year part.

Can someone help me?

标签: javaandroidsqlsqlite

解决方案


If you want the date to have a specific month (without having constraints on the year or the date), you can continue to use the strftime function. Querying results for October:

WHERE strftime('%m', COLUMN_DATE) = '10'                  /* October */

If you want to query results for a specific month-year, you can combine both of them like this:

WHERE strftime('%Y-%m', COLUMN_DATE) = '2011-05'          /* May, 2011 */

推荐阅读