using sqlite's strftime function in android

Mano picture Mano · Aug 30, 2012 · Viewed 8.5k times · Source

Hello I have inserted some dates in my database.

When I use sqlite3 in the format YYY-MM-DD in another table when I do my query like

SELECT * 
FROM SDay 
WHERE  strftime('%Y-%m-%d', date)>=strftime('%Y-%m-%d','2013-02-21')
    AND  strftime('%Y-%m-%d', date)<strftime('%Y-%m-%d','2013-09-15')

it works just fine.

On the other hand when i use the format YYY-MM in another table then the

SELECT * 
FROM SMonth 
WHERE  strftime('%Y-%m', date)>=strftime('%Y-%m','2013-02') 
    AND  strftime('%Y-%m', date)<strftime('%Y-%m','2013-09')

will not work.Why does it happen and how could I fix it?

Thank you.

Answer

user610650 picture user610650 · Aug 30, 2012

In your second statement, strftime is provided with an invalid timestring. This causes the function to return NULL:

sqlite> .nullvalue <NULL>
sqlite> select strftime('%Y-%m','2013-02');
<NULL>

Just add the day to the date you provide and it will work properly:

sqlite> select strftime('%Y-%m','2013-02-01');
2013-02

You can find a list of valid timestring formats here.