Alternative to STR_TO_DATE() in sqlite

自古美人都是妖i 提交于 2020-05-23 13:02:20

问题


What is an alternative in SQLite as STR_TO_DATE() funciton in MySQL?


回答1:


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


来源:https://stackoverflow.com/questions/1858275/alternative-to-str-to-date-in-sqlite

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