7

I have table which contains data along with created on date. I want to select data from table according to two given date ranges, or of one particular month.

I am not sure how to do that, as created on column is of type text and is stored as dd-MM-yyyy HH:mm:ss

I am using rawQuery() to fetch data.

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118

2 Answers2

16

You can do something like this:

 mDb.query(MY_TABLE, null, DATE_COL + " BETWEEN ? AND ?", new String[] {
                minDate + " 00:00:00", maxDate + " 23:59:59" }, null, null, null, null);

minDate and maxDate, which are date strings, make up your range. This query will get all rows from MY_TABLE which fall between this range.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
  • 2
    `mDb.rawQuery("select * from " + MY_TABLE + " where " + DATE_COL + " BETWEEN " + minDate + " 00:00:00 AND " + maxDate + " 23:59:59");` – Tyler Treat Mar 10 '12 at 21:46
  • I have date from 11th March to 14th March, but when I am using query, "select * from category where createdon BETWEEN '11-03-2012 00:00:00' AND '12-05-2012 23:59:59'", it should show me data of all dates, as my month is set to May, but its only showing me 11th and 12th March data :( – Adil Bhatty Mar 10 '12 at 22:00
  • 1
    I have done like this "select * from category where date(createdon) between date('2012-03-11') AND date('2013-01-01');" it seems like working :) – Adil Bhatty Mar 10 '12 at 22:13
  • what is DATE_COL and what is the format of minDate and maxDate ?? – abidinberkay Apr 16 '13 at 10:37
  • @TylerTreat u had store date like 2013-01-01 in this formate but in my case 2013/01/01 this formate so it's not working – CoronaPintu Aug 18 '14 at 11:13
0

Here is some usefull

 //Get Trips Between dates
public List<ModelGps> gelAllTripsBetweenGivenDates(String dateOne, String dateTwo) {
    List<ModelGps> gpses = new ArrayList<>();
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    final String columNames[] = {DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME, DBHelper.COLUMN_LATITUTE, DBHelper.COLUMN_LONGITUDE, DBHelper.COLUMN_ALTITUDE, DBHelper.COLUMN_DATE, DBHelper.COLUMN_TYPE, DBHelper.COLUMN_TRAVEL, DBHelper.COLUMN_SPEED};
    String whereClause = DBHelper.COLUMN_TYPE + " = ? AND " + DBHelper.COLUMN_DATE + " BETWEEN " + dateOne + " AND " + dateTwo;
    String[] whereArgs = {"Trip"};

    Cursor cursor = database.query(DBHelper.TABLE_NAME_GPS, columNames, whereClause, whereArgs, null, null, DBHelper.COLUMN_NAME + " ASC");
    while (cursor.moveToNext()) {
        ModelGps gps = new ModelGps();
        gps.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
        gps.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)));
        gps.setLatitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LATITUTE)));
        gps.setLongitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LONGITUDE)));
        gps.setAltitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_ALTITUDE)));
        gps.setDate(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DATE)));
        gps.setType(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TYPE)));
        gps.setTravel(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TRAVEL)));
        gps.setSpeed(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_SPEED)));
        gpses.add(gps);
    }
    database.close();
    cursor.close();
    return gpses;
}
Samir
  • 6,405
  • 5
  • 39
  • 42