【问题标题】:Retrieving data given month wise按月检索数据
【发布时间】:2013-12-28 08:38:49
【问题描述】:

我将用户在 sqlite 中选择的一些数据和日期以 dd-MMM-yyyy 格式存储在 storeDate 命名列中,类型为 TEXT,因为 afaik 没有 DATE直接输入。现在我想按月检索存储的数据。假设我输入 01-Dec-2013 并构建 where 子句,那么我需要获取该特定月份(2013 年 12 月)的数据。有人可以建议如何实现这一目标吗?我目前的查询是:

Cursor cursor= db.rawQuery("SELECT * FROM "+TABLE_NAME+" "+whereClause+" GROUP BY "+CATEGORY, null);

where子句的地方应该给出什么?

【问题讨论】:

    标签: android sqlite date


    【解决方案1】:

    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

    【讨论】:

    • 我想把条件放在where子句中,where子句应该根据给定的日期生成。
    • 感谢您的宝贵时间,但我没有得到输出 :( 该列是否需要以 YYYY-MM-DD 格式存储日期(字符串类型)?
    • 最后一个问题,我们如何在 query() 方法中使用 this 而不是 rawquery(),因为我必须使用 query 或框架提供的其他方法而不是 rawquery。
    【解决方案2】:

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

    SELECT * FROM tbl WHERE MONTH( trans_date ) = 3
    

    【讨论】:

    • 对不起,我的问题是关于 sqlite 的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    相关资源
    最近更新 更多