开发者

Alternative to STR_TO_DATE() in sqlite

What is an alternative in SQLite as STR_TO_DATE() fu开发者_如何学Pythonnciton in MySQL?


I know that this post is somewhat outdated but I am posting anyways for those who might have a similar issue as I did. I was getting a date from an open api as "2013-01-01T01:00:00+0000" and storing it as a string in sqlite. Problem arose when I needed some way of querying the records based on date range. Since I wasn't able to use STR_TO_DATE() I found that I could use the sqlite function strftime(). Below is an example of a working query that I am using for this instance , hopefully it will be able to help someone else out:

 select strftime(date_created) as dateCreated from tblFeeds 
 where strftime(date_created) between strftime('2013-01-01') and strftime('2013-01-08')
 order by dateCreated;

This got me the records that were created between 01-01-2013 and 01-08-2013 (7 date range).

Also here are some date ranges for you in case you don't know the dates you want to use:

(Last 24 hours):

select strftime(date_created) as dateCreated from tblFeeds 
where strftime(date_created) between  strftime(date('now','-24hours')) and  strftime(date('now'))
order by dateCreated

(Last week):

select strftime(date_created) as dateCreated from tblFeeds 
where strftime(date_created) between  strftime(date('now','-7days')) and  strftime(date('now'))
order by dateCreated

(Last month):

select strftime(date_created) as dateCreated from tblFeeds 
where strftime(date_created) between  strftime(date('now','-1months')) and  strftime(date('now'))
order by dateCreated
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜