using sqlite's strftime function in android

て烟熏妆下的殇ゞ 提交于 2020-01-14 11:56:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/12193593/using-sqlites-strftime-function-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!