如何根据日期和日期从Sqlite获取记录

时间:2017-04-08 16:09:03

标签: android sqlite

我正在尝试根据日期和日期从SQLite数据库中获取记录。 例如:在2月2日至3月5日之间获取有生育日期的记录。 问题是DOB列还存储了人的生日年份,我不想考虑年份来获取记录。 在数据库中存储DOB的格式为DD-mm-YYYY(02-02-1990)。

2 个答案:

答案 0 :(得分:1)

创建其他2列,仅存储日和月。然后查询示例是

select * from table1 where day between  1 and 5 and month between 2 and 5

答案 1 :(得分:1)

//Concatenate MonthDayFields and use between statement to fetch records

// This Query will Fetch Records From 5 Oct to 5 Apr 

SELECT *,substr('00'||DOB_Month,-2,2) || substr('00'||DOB_Day, -2, 2)
as 'myField' FROM Table1
WHERE (myField BETWEEN "1005" AND "1231") OR (myField  BETWEEN "0101" AND "0405")


// This Query will Fetch Records From 5 Apr to 18 Oct

SELECT *,substr('00'||DOB_Month,-2,2) || substr('00'||DOB_Day, -2, 2)
as 'myField' FROM Table1
WHERE myField between "0405" and "1018"