Getting MAX Date in SQLite

I use SQLite as a data store for my Android application. I created one table with a datetime column. When I insert notes or select sentences, I use the dd.MM.yyyy format (08/10/2012) for dates.

Now I have a problem getting the line with the last date / MAX. I tried using the MAX statement (date_column), but it returns an invalid date. I have the following dates in my desk, and it returns on July 31, 2012 instead of August 4, 2012. Does 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 the piece of code:

 String selection = "measurementDate = (SELECT MAX(measurementDate) FROM Measurements)"; Cursor cursor = database.query("Measurements", allColumns, selection, null, null, null, null); cursor.moveToFirst(); ... 
+4
source share
2 answers

This is probably due to the fact that the comparison is performed with the saved string, and not with it.

You can save the string in different ways, YYYYMMDD, which should compare them correctly or, if you can change the table, store dates as milliseconds and format back to the string when necessary.

This can help

+2
source

Could this be for 31?
Try to find out if this helps:

 SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1 
+3
source

All Articles