Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the MAX date in SQLite

Tags:

android

sqlite

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();
...
like image 438
Ismar Slomic Avatar asked Aug 09 '12 23:08

Ismar Slomic


People also ask

What is Max date in SQL?

Remarks. The maximum valid date for a SqlDateTime structure is December 31, 9999.

How can find max and min date in SQL?

The SQL MIN() and MAX() FunctionsThe MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.

What is use of SQLite Max aggregate function?

The SQLite MAX function is an aggregate function that returns the maximum value of all values in a group. You can use the MAX function to accomplish a lot of things. For example, you can use the MAX function to find the most expensive products, find the biggest item in its group, etc.


1 Answers

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

SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1
like image 96
Ervi B Avatar answered Sep 23 '22 02:09

Ervi B