Govind
Govind

Reputation: 143

Getting data between two dates

I want to retrieve data between two dates in sqlite db.I have tried using BETWEEN clause,it works fine if dates are of same month like '28-10-2012' and '29-10-2012'

But if dates are of different like '30-10-2012' and '1-11-2012' month its not displaying data

Please help me out.

Upvotes: 0

Views: 1624

Answers (4)

Akanksha Rathore
Akanksha Rathore

Reputation: 3623

cursor = sdb.query(TABLE_INFORMATION, Info_Date_Time_Array, DATE+" BETWEEN ? AND ?", new String[]{mindate,maxdate}, null,
                    null, null);

Upvotes: 0

tang
tang

Reputation: 31

I suggest the date write in the SQLite by millisecond.

First you read all dates write in ArrayList dates

Calendar cal=Calendar.getIntance();
cal.set(yyyy,mm,dd);
dates.add(cal.getTimeInMillis());

Second you can compare the two dates

long start_time;
long end_time;
Calendar cal=Calendar.getIntance();
start_time=cal.set(2012,9,30);
end_time=cal.set(2012,10,1);
for(int i=0,i<dates.size();i++)
{
    if(dates.get(i)>start_time&&dates.get(i)<end_time)
     {......`enter code here`}
}

Upvotes: 1

Anuj Sharma
Anuj Sharma

Reputation: 488

Its would be working for the same months because sql's defult concept for getting data between two dates works only where the date format is like YY/MM/DD ..... in your case you need to use other attribute too rather than using only date entry...

Upvotes: 2

Chirag
Chirag

Reputation: 56925

Try this Query.

return db.query(TABLE_NAME, new String[]{"temp_sp_date","temp_sp_amount"},
                        "(temp_sp_date >= ? and temp_sp_date <= ? )" , new String[] {startDate,endDate}, " strftime(" + "\"%d" + "-" + "%m" + "-" + "%Y\""  + ",temp_sp_date) ",null , null);

Here temp_sp_date is column from my table and startDate and endDate are date paramtere.

Upvotes: 0

Related Questions