0

I am developing android application. One of my application table has "foo_date" as column name. and data type is TEXT. I have following data in my table

primary key  | foo_date  | foo_value
--------------------------------------
1             2013-3-1    100

2             2013-3-2    2

3             2013-3-4    100

4             2013-3-3    1000

my requirement is to get data for the current week(i.e. 2013-3-4 to 2013-3-10). When i write below query, i am getting all data except row # 3

select * from foo_tbl where foo_date between '2013-3-4' and '2013-3-10';

How do I write sqlite query so that it includes all data for current week?

Thansk Chintan

4

2 回答 2

0

您的问题是您以无法轻松比较的格式存储了日期;字符1在前4,所以2013-3-10比较小于2013-3-4

文档所述,您应该使用 ISO 日期格式yyyy-mm-dd。使用恒定的字段大小,您可以使用具有有效字符串比较的查询:

SELECT * FROM foo WHERE foo_date BETWEEN '2013-03-04' AND '2013-03-10'

或使用日期函数来计算星期:

SELECT * FROM foo WHERE strftime('%W', foo_date) = '09'
于 2013-03-04T18:36:45.603 回答
0

首先,将日期存储为标准 sqlite 时间字符串格式(即 YYYY-MM-DD)。然后,你可以使用这个:

select * from foo where strftime('%W', foo_date) == strftime('%W', 'now')

选择与 now 具有相同周行。

于 2013-03-04T18:43:15.947 回答