Search code examples
fluttersqlitesqflite

SQFLITE - Get records from yesterday and previous dates | Get records for tomorrow and future dates


Note: (No need to answer) I'm writing this for my future self in case I forget and others who might need it

I'm trying to get records plotted yesterday and older and records plotted for tomorrow and the following days. The year, month, and day values for the plotted dates are separated in 3 columns (year, month, day). For example, the date for an event plotted tomorrow is stored in the db under the following columns as INTEGERS:

year - 2022
month - 1
day - 26

Here's what I have so far:

SELECT * from events 
WHERE DATE(year-month-day) < '2022-01-26'

The problem in the code above is it doesn't filter the dates. Plotted events tomorrow and other future events are being included.


Solution

  • The following worked for me / provided the results I needed:

    Change the date being compared to from String to DATE

    For older dates:

    SELECT * from events 
    WHERE DATE(year-month-day) > DATE(2022-01-26)
    
    • DATE(year-month-day) - value being evaluated

    • DATE(2022-01-26) - old / previous date for comparison

    • The above example will return the events from 2022-01-25 and older

    ################################################################

    For future dates:

    SELECT * from events 
    WHERE DATE(year-month-day) < DATE(2022-01-26)
    
    • Will return events from 2022-01-27 and later