每月检索数据

时间:2013-12-28 08:38:50

标签: android sqlite date

我正在storeDate TEXT名为DATE类型的列中以 dd-MMM-yyyy 格式存储用户在sqlite中选择的一些数据和日期,因为afaik有没有01-Dec-2013直接输入。现在我想逐月检索存储的数据。假设我将输入作为Cursor cursor= db.rawQuery("SELECT * FROM "+TABLE_NAME+" "+whereClause+" GROUP BY "+CATEGORY, null); 并构建where子句,那么我需要获取该特定月份的数据(2013年12月)。有人可以建议如何实现这一目标吗?我目前的疑问是:

{{1}}

在whereClause的地方应该给出什么?

2 个答案:

答案 0 :(得分:2)

SQLite does not support month names,遗憾的是。您必须使用查找表,case语句或表示层上的开关将其转换为月份名称。

试试这个

SELECT strftime('%Y-%m', table_column) FROM `table name` WHERE strftime('%Y-%m',  table_column) = '2013-04'

如果您只想打印月份名称,请尝试此

SELECT case  strftime('%m',date_column) when '01' then 'January' when '02' then 'Febuary' when '03' then 'March' when '04' then 'April' when '05' then 'May' when '06' then 'June' when '07' then 'July' when '08' then 'August' when '09' then 'September' when '10' then 'October' when '11' then 'November' when '12' then 'December' else '' end
as month FROM `your_table` WHERE   strftime('%m',date_column)='12'

在SQLite

中加入更多日期和时间函数

Date And Time Functions

答案 1 :(得分:0)

您可以使用MySQL的MONTH()函数

SELECT * FROM tbl WHERE MONTH( trans_date ) = 3