Android SQLLite last 31 days data from current_timestamp column - android

I am trying to pull last 31 days data from SQLLite db, using below SQL. I already searched google and tried various options, while all SQL works i am not getting results as expected. For example in below screenshot you will see i do have record on 18-Sep but sql doesn't return any results...I am not sure what i am missing here..
SELECT * FROM transactions WHERE TIMESTAMP > (SELECT DATETIME('now', '-30 day'))

Use between instead of Timestamp comparison directly -
SELECT * FROM transactions WHERE TIMESTAMP BETWEEN datetime('now', '-31 days') AND datetime('now', 'localtime')
This is because SQLlite stores dates in String format and doesn't have a standard DateTime format. So The direct comparison of String would fail. Hence using of the datetime function to compare dates with existing values helps.

You can also try as
SELECT * FROM TRANSACTIONS WHERE TIMESTAMP > datetime('now', '-30 days')

Related

Filtering SQLite records using strftime

I have SQLite database in Android app with records like sample below
id date
1 2023-01-29T15:56:00.733533+01:00
2 2023-01-29T15:56:02.092214+01:00
I need to filter these records by days, month, etc. I found that strftime function should the best way to do this, but my SELECT is not working. Can someone please help what I'm doing wrong.
My SELECT is
SELECT * FROM history WHERE date = strftime('%Y-%m-%d', '2023-01-29');
Thank you.
The time is stored in ISO-8601 standard format
Do this instead
SELECT * FROM history WHERE strftime('%Y-%m-%d', date)='2023-01-29'
//for a particular month
SELECT * FROM history WHERE strftime('%m', date)='2'
With strftime() you can format your timestamps to a specific format and the correct syntax is this:
SELECT *
FROM history
WHERE strftime('%Y-%m-%d', date) = '2023-01-29';
or, for a specific month like '2023-01':
SELECT *
FROM history
WHERE strftime('%Y-%m', date) = '2023-01';
or, for a specific year like '2023':
SELECT *
FROM history
WHERE strftime('%Y', date) = '2023';

Comparing dates in android sqlite database

I'm comparing two dates in android sqlite database
I stored dates in the format
YYYY-MM-DD
Cursor cursor=db.rawQuery("SELECT * FROM "+tableName+" WHERE SALESDATE BETWEEN '2020-1-01' AND '2020-2-01';",null);
It gives result with dates of month 10, 11 and 12 along with the dates between above specified dates.
I would like to know if it is a bug or is there any mistake in my code.
The problem here is that your date literals are in a non standard (and likely incorrect) format. Appreciate that the following inequality holds true:
'2020-10-01' > '2020-1-01' AND '2020-10-01' < '2020-2-01'
This is true because the text 10 is lexicographically larger than just 1, but also less than 2. To avoid this problem, use proper date literals:
String sql = "SELECT * FROM " + tableName + " WHERE SALESDATE BETWEEN '2020-01-01' AND '2020-02-01';"
Cursor cursor = db.rawQuery(sql, null);
Note that SQLite does not actually have a formal date type. Thus, it is very important to always store your dates in SQLite using a proper ISO format.
You should store your Date as long value in database. Simple new Date().getTime() gives you this value and new Date(long value) returns it back. So you can make such queries easy.
But what I can suggest is to:
Export your table to CSV,
Change the date values to a proper SQLite TimeString and
Re-import the CSV after deleting the original table.
Then, you can run a query like:
SELECT * FROM tableName WHERE SALESDATE BETWEEN '2020-01-01 00:00:00' AND '2020-02-01 23:59:59'

How I can get all last year's records of SQLite table

I have a table in which each record has a date. I want to get records based on the last 365 days.
WHERE MYDATE < date('now', '-365 day') is not working
The table has the following:
George 10/4/2016 Pass
John 4/5/2015 Pass
John 19/7/2013 Fail
So I run the above and
I get John with 19/7/2013 and George with 10/4/2016
You should save dates in SQLite format
SELECT
Name
FROM
Persons
WHERE
date <= date('now', '-365 days')
maybe do the job

SQLite query between dates

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?
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');
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

SQLite date - find event from today

I would like to retrieve from a SQLite database a list of events which have their start_time "today". start_time contains a timestamp e.g. like this 1338613200000.
I tried this:
SELECT * FROM events WHERE date(start_time, 'unixepoch')=date('now')
Doesn't work...
I achieved the same thing in MySQL with such a statement:
SELECT * FROM events WHERE DATE(start_time)=CURDATE();
EDIT:
It's the problem with my timestamps. They have three zeros too much
Java uses milliseconds since the Unix epoch, whereas I'm guessing SQLite uses seconds.
Divide System.currentTimeMillis() by 1000 before querying the database.
You can try something like this:
SELECT * FROM events
WHERE datetime(start_time,'unixepoch', 'localtime')
BETWEEN DATE('now') AND DATE('now', '+1 day')
See more details in here
Try
Calendar c= Calendar.getInstance();
long l=c.getTimeInMillis();
and do something like the following:
SELECT * FROM events WHERE start_time=l;
see if that works.

Categories

Resources