1

I am comparing time in android sqlite database, here is my table structure in the screen shots. Actually I have two times, like 8:00 and 7:30 (basically these are employee shift timings) in the string and I have a column c_TIME in my database and I want all the records who lies in b/w my two time i.e.(8:00 and 7:30) from c_TIME.

I have used this query but it is not working, please anyone guide me what I am doing wrong

SELECT *
FROM ATTENDANCE
WHERE strftime('%H','08:00') > strftime('%H', c_TIME) AND
      time('08:00') < strftime('%H', c_TIME) AND
      STATE = 'IN' 

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Achin
  • 1,252
  • 4
  • 28
  • 63

2 Answers2

0

The following should work:

SELECT *
FROM ATTENDANCE
WHERE strftime('%H:%M:%S', c_date) > '07:30:00' AND
      strftime('%H:%M:%S', c_date) < '08:00:00' AND
      STATE = 'IN'

Comments:

Instead of using the c_TIME field, you should use c_date. Actually, I don't see any value in keeping around a column which stores the time separately.

strftime returns a string based on some sort of datetime/timestamp input. The output from strftime('%H:%M:%S', c_date) should have the form HH:MM:SS, and this string can be compared lexicographically against 08:00:00 and 07:30:00. The comparison should still work, because for strings of the same length it should still sort numerically.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Try:

select * from ATTENDANCE where c_date between 'yyyy-mm-dd' and 'yyyy-mm-dd';

Instead of using c_TIME,use c_date field.

You can change the date format in the query accordingly to retrieve data between two specific times.

Sqlite supports following formats:

YYYY-MM-DD

YYYY-MM-DD HH:MM

YYYY-MM-DD HH:MM:SS

YYYY-MM-DD HH:MM:SS.SSS

YYYY-MM-DDTHH:MM

YYYY-MM-DDTHH:MM:SS

YYYY-MM-DDTHH:MM:SS.SSS

HH:MM

HH:MM:SS

HH:MM:SS.SSS

now

DDDDDDDDDD 

See here

kgandroid
  • 5,507
  • 5
  • 39
  • 69