0
select * from table_my where my_date between '2014-07-23 00:00:00' and '2014-07-30 00:00:00' order by _id desc

Here is my query, I d like to create this simple date compare query, but not working. This query don't add anything out. Why? How can I compare dates in sqlite?

3
  • SQLite does not have a DateTime datatype! To compare dates get the epoch time from the Date with getTime() (it's a long) and save that instead in the database. Comparing two numbers is obviously very easy. Commented Jul 30, 2014 at 12:24
  • 2
    stackoverflow.com/questions/1469689/… Sometimes answers can come about through a google search. Commented Jul 30, 2014 at 12:26
  • 2
    @bhuvan-venkatesh and sometimes SQLite behaves different than MySQL. Commented Jul 30, 2014 at 13:27

2 Answers 2

2

Try below query using strftime date functions.

SELECT * from table_my
WHERE strftime('%Y-%m-%d', my_date )
     between  
          strftime('%Y-%m-%d', '2014-07-23') 
          and 
          strftime ('%Y-%m-%d', '2014-07-30');

EDIT:

To use with hours and minutes, you can try something like:

SELECT * from table_my
WHERE strftime('%Y-%m-%d  %H:%M', my_date )
     between  
          strftime('%Y-%m-%d  %H:%M', '2014-07-23 11:22') 
          and 
          strftime ('%Y-%m-%d  %H:%M', '2014-07-23 11:25');
Sign up to request clarification or add additional context in comments.

4 Comments

It looks like this working, but Can u help me how to use this to use minutes in this query? So: yyyy-mm-dd HH:MM (2014-07-23 11:22 and between 2014-07-23 11:25)
You can use strftime('%Y-%m-%d %H:%M', my_date )
@lacas: Refer edit. Please upvote and accept as answer if it helped you..
Thanks now it is working, my code look like this String sql = "SELECT * from "+SQLLiteHelper.TABLE_MY +" WHERE strftime('%Y-%m-%d %H:%M:%S', mdate ) between strftime('%Y-%m-%d %H:%M:%S', '"+nnow+"') and strftime ('%Y-%m-%d %H:%M:%S', '"+nnowminus+"')";
0
select * from table_my where my_date between '2014-07-30' and '2014-07-23' order by _id desc

use above query may it will work to you i also got such kind of problem during coding i think it is coming cause your using lower value before greater value

may it will help you just chek it

i just swiped the two dates also i removed timestamp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.