Skip to content Skip to sidebar Skip to footer

How To Get The Closest Dates In Sqlite Database To Present Date

I have an SQLite database with a table that holds a column full of dates. I need to get the date closest to the present date out of everything in that column, but I don't know how

Solution 1:

Test this:

SELECT t.*FROMtable t
WHERE t.date_col = 
  ( SELECTMIN (t2.date_col) FROMtable t2
    WHERE t2.date_col >= ? );

? parameter will be passed with value is CURRENT date

IF date_col data type is Integer in SQLite, ? parameter will be passed with this value: System.currentTimeMillis()

Note: You may have to remove time info such as hour, minute, second, millisecond from System.currentTimeMillis()

Note 2: If you use String as Data type in SQLite, you have to format System.currentTimeMillis() into Date format "yyyy/MM/dd". If you use other formats such as M/d/yyyy --> You will have date String comparing issues. See issue below for M/d/yyyy format:

"5/15/2015".compareTo("11/30/2015") ---> Return 4 > 0
--> means "5/15/2015" > "11/30/2015" --- Wrong

Post a Comment for "How To Get The Closest Dates In Sqlite Database To Present Date"