0

I'm trying to select a specific date between two dates. The forma its stored in the DB is M/d/yyyy. What I'm trying to do, is passing two dates, and I want to select all data of a column between this two dates. I know there is data in the DB, but it doesnt bring anything for some dates.For some dates, it brings the data, but for some dates it brings the data.

Example:

When I choose 7/10/2011 as first date, and as second date 7/16/2011, it brings the data. But When I choose 7/9/2011 as first date and as second date 7/16/2011, it doesnt bring anything.

Here is my query:

Cursor cursor = db.query(CONTACT_DATA, new String[] { "duration" },
                "date >= ? AND date <= ?",
                new String[] { firstDate, secondDate }, null, null, null);

If anyone can help with this question. Thanks!

rogcg
  • 10,451
  • 20
  • 91
  • 133

2 Answers2

0

SQLite does not have a native date type; it is comparing your dates as strings. That's why when the dates are in the same month and year, and have the same number of digits in the day, the query appears to work, but that is just luck.

Typically SQLite users format their date strings using ISO-8601 format (YYYY-MM-DD) which sorts correctly as strings.

SQLite does have date functions -- if your dates are all well formed you may be able to use these, along with ltrim and rtrim to convert your dates to a format usable in queries.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
0

I'm gonna store the dates as miliseconds(epoch format), so its easier to compare. And when printing the date, I'll convert it.

rogcg
  • 10,451
  • 20
  • 91
  • 133