6

Im using SQLite as data storage for my Android App. I have created one table with column of type datetime. When I do the insert of records or selects statements I use the format dd.MM.yyyy (10.08.2012) for dates.

Now, I have issue with getting the row with the latest / MAX date. I have tried using the MAX(date_column) statement, but it returns wrong date back. I have following dates in my table and it returns 31.07.2012 in stead of 04.08.2012. Anyone know what I'm doing wrong?

04.08.2012
03.08.2012
02.08.2012
01.08.2012
31.07.2012
30.07.2012

Here is part of the code:

String selection = "measurementDate = (SELECT MAX(measurementDate) FROM Measurements)";
Cursor cursor = database.query("Measurements", allColumns, selection, null, null, null, null);
cursor.moveToFirst();
...

2 Answers 2

4

Could it be because of 31?
Try to see if this helps:

SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1
1
  • That didn't help! See my reply above Aug 10, 2012 at 7:09
3

It's probably due to the comparison being made on the stored string rather than treating it as a date.

You could store the string in differently, YYYYMMDD, which should compare them correctly or, if you can modify the table, store the dates as milliseconds and format back to a string when needed.

This might help

1
  • Tnx, you were right! All dates were placed in format mm.DD.yyyy and the comparation failed. The strange thing is that sqlite.org says that there is no DateTime datatype in sqlite, but when I created my table I used that datatype and create statement didn't failed. :S Anyway, i switched to integer datatype and I put all dates as milliseconds. It works now! Aug 10, 2012 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.