替代sqlite中的STR_TO_DATE()

时间:2009-12-07 06:46:38

标签: sqlite

SQLite中的另一种选择是MySQL中的STR_TO_DATE()函数吗?

2 个答案:

答案 0 :(得分:3)

我知道这篇文章有些过时,但我仍然会为那些可能和我有类似问题的人发帖。我从开放的api获取日期为“2013-01-01T01:00:00 + 0000”并将其存储为sqlite中的字符串。当我需要某种方式根据日期范围查询记录时出现问题。由于我无法使用STR_TO_DATE(),我发现我可以使用sqlite函数strftime()。下面是我为此实例使用的工作查询的示例,希望它能够帮助其他人:

 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;

这让我得到了在01-01-2013和01-08-2013(7个日期范围)之间创建的记录。

如果您不知道要使用的日期,还有一些日期范围:

(过去24小时):

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

(上周):

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

(上个月):

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

答案 1 :(得分:0)