Hi i need to do a query where I compare 2 dates and select all the objects created between those 2 dates.
My date field is a Text type.
I have run some tests and apparently the date is being stored in YYYY-MM-DD format.
Im using this code to do the query:
return bd.query("Gasto", null , "Fecha_Creado between '"+fecha1+ "' and '"+fecha2+"'", null, null, null, "Fecha_Creado DESC", null);
and this code to retrieve the dates and pass them to the previous function
return bd.obtenerGastosVar(fecha.get(Calendar.YEAR)+"-"+(fecha.get(Calendar.MONTH)+1)+"-1",
fecha.get(Calendar.YEAR)+"-"+(fecha.get(Calendar.MONTH)+1)+"-"+fecha.getActualMaximum(Calendar.DAY_OF_MONTH), ordenadoPor);
so the final result should a query that is saying :
select * from gasto where date between date1 and date2
so thats
select * from gasto where date between 2013-8-1 and 2013-8-31
so, that query is supposedly making a date comparison with the former YYYY-MM-DD.
Im expecting objects created between those two dates, but its NOT working. Its was working an hour ago
I did something funny and use for the query dates, a DD-MM-YYYY format. And that apparently is working, but im scared that using this format for the queries will bring not reliable results.
So how can I fix this and still get reliable results?
Thanks for your help
Apparently, for me to be able to compare the dates, achieving good results, dates must have 2 digits months and days
for example 2013-04-09 or 2013-11-25
dates with one digit months or days don't work
Related
I am trying to create a simple reminder app. Im just logically thinking about the process. Basically I want to be able to choose a DAY and time e.g Monday 15:00, this will trigger EVERY Monday 15:00 until it gets deleted from database. Having said that example I have questions to accomplish this process.
How will I store DAY and TIME, what type, do I need different columns in my table?
How can I compare real time DAY to current DAY, so if its Monday real time it will return ONLY Monday reminders? is this possible?
Will I need to primarly focus using calendar?
As documentation says:
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values.
You can store date and time in the TEXT type of field in the following format:
YYYY-MM-DD HH:MM:SS.SSS
and then use built-in date & time functions of SQLite to filter your records.
Another option is to store date & time as a time-stamp in milliseconds and write proper queries for selecting data or use Joda library for filtering or date & time transformation, but probably such solution would be less efficient and less convenient than first option.
Using integer column is the easiest solution.
Just store the date in millisecond (Calendar.getTimeInMillis()) and your good to go.
Then you just have to search on that integer to find the correct event in your database :
String selectQuery = "SELECT whateveryouneed FROM events WHERE date_event > ?";
Cursor c = db.rawQuery(selectQuery, new String[] { String.valueOf(calendar.getTimeInMillis())});
...
if you need to find all the event for a day , you just have to find the limits of the day in millisecond and make a query according to those limits
I am keeping date time values(Unix timestamps) in a "NUMERIC" format in SQLite table, the value is calculated through Java Date function:
new Date().getTime();
The values look like proper date/time when I load these in an Android program, but when I try to experiment with queries through SQLite data browser 2 beta the results are awkward. Numeric values are given below:
1391313058888
1391313104336
1391313175752
When I try to apply date function the SQLite data browser shows following for all three rows:
-1413-03-01 13:07:12
My query is
SELECT date(trxDateTime, 'unixepoch') from trx_log
I was trying to figure out how to get correct date values in the first place, once I got those then I believe I could figure a way to use it in where clause or group by.
Basically I am trying to show totals sales by year. Any help will be appreciated.
Your times are in milliseconds. Just convert them to seconds, and you'll be fine:
SELECT date(trxDateTime / 1000, 'unixepoch') from trx_log
I have seen number of post about storing date.
I am still not getting the fine and exact approach about saving it to a sqlite database.
I am able to store it, but during sorting I need to consider only month and day just like birthday where years doesn't matter.
What will be the query if I want to get the row whose date is 2 or 3 days in advance, like 2nd march row if searched on 28 Feb?
You should start by checking out the SQLite documentation of date & time functions.
For instance, to solve your problem "And what will be the query if i want to get the row whose date is 2 or 3 days in advance" you'd use julian day calculations, such as this example that you can execute directly in the sqlite3 shell:
create TABLE example (_id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL);
insert into example (date) values ('2011-01-02');
insert into example (date) values ('2011-04-02');
insert into example (date) values ('2012-02-26');
insert into example (date) values ('2012-02-27');
insert into example (date) values ('2012-02-28');
insert into example (date) values ('2012-02-29');
insert into example (date) values ('2012-03-01');
insert into example (date) values ('2012-03-02');
insert into example (date) values ('2012-03-03');
select date from example where julianday(date) - julianday('now') < 3 AND julianday(date) - julianday('now') > 0;
This would return (given that "today" is feb 28th) all the days that are one, two or three days in the future:
2012-02-29
2012-03-01
2012-03-02
Edit: To only return rows, regardless of year, you could do something like this - using a VIEW (again, exampl is directly in SQLite):
create view v_example as select _id, date,
strftime("%Y", 'now')||'-'||strftime("%m-%d", date) as v_date from example;
This VIEW would return the date & times in your database "rebased" on the current year - which, of course could introduce all manner of wonky behavior with leap years.
You can select all the dates like this in that case:
select date from v_example where
julianday(v_date) - julianday('now') < 3 AND
julianday(v_date) - julianday('now') > 0 ORDER BY v_date;
Which would return:
2012-02-29
2012-03-01
2001-03-01
2012-03-02
2010-03-02
If you want to sort by day and month consider storing the date as string in the format ddMMyyyy (you need two digits for day and month, otherwise the sorting will be flawed). Sorting by increasing values will give you dates sorted by day and month (and then year).
You can even do range query with string but you have to compute the query string.
Alternatively you may store the date as milliseconds in an additional column (this is the usual format for dates in the database) and do the range queries more easily with integer arithmetic.
One option is to use strftime() in SQLite to strip of the year and then do a comparison.
select * from sometable where strftime('%m-%d', somecolumn) = '02-28'
This will do a query of all rows for February 28th. But performance might be hurt if you have a large table and need to do a string conversion of every row for comparison. Maybe store the day and month in two additional columns if this query is performed often?
I'm trying to select a specific date between two dates. The forma its stored in the DB is M/d/yyyy. What I'm trying to do, is passing two dates, and I want to select all data of a column between this two dates. I know there is data in the DB, but it doesnt bring anything for some dates.For some dates, it brings the data, but for some dates it brings the data.
Example:
When I choose 7/10/2011 as first date, and as second date 7/16/2011, it brings the data. But When I choose 7/9/2011 as first date and as second date 7/16/2011, it doesnt bring anything.
Here is my query:
Cursor cursor = db.query(CONTACT_DATA, new String[] { "duration" },
"date >= ? AND date <= ?",
new String[] { firstDate, secondDate }, null, null, null);
If anyone can help with this question. Thanks!
SQLite does not have a native date type; it is comparing your dates as strings. That's why when the dates are in the same month and year, and have the same number of digits in the day, the query appears to work, but that is just luck.
Typically SQLite users format their date strings using ISO-8601 format (YYYY-MM-DD) which sorts correctly as strings.
SQLite does have date functions -- if your dates are all well formed you may be able to use these, along with ltrim and rtrim to convert your dates to a format usable in queries.
I'm gonna store the dates as miliseconds(epoch format), so its easier to compare. And when printing the date, I'll convert it.
I am using SQLite database for my application.
The Table Structure Goes Like :
_id : integer primary key
name : text
day : date
I am able to store date in format : dd-mmmmm-yyyy eg. 15-June-2011
But when i tried to retrieve all records filtered by date from the database it returns me null.
database.query(DATABASE_TABLE, new String[] { "strftime('%d-%mm-%Y',date('now'))","strftime('%d-%m-%Y',"+KEY_DAY+")" },
"strftime('%d-%m-%Y',date('now'))=" + KEY_DAY , null,null,null,null,null);
It didnt match with anyrow's date even though there were some matching dates.
I have already gone thru documentation of SQLite. But didn find any solution yet.
I want to have something like :
select * from table where day=curdate();
How can i do the same task in SQLite ?.
(Yes I am flexible to change the format of date stored in Dateabase)
What are other alternatives for the same task ?.
In java programming you can convert any date format into long (time in milliseconds) and viceversa. My opinion is while storing format the date into long format in java and then store long value of date in database. also while retrieving you can retrieve the long value and then format that as per your expected date format. I have been using this type of logic for several application.
Thanks
Deepak.
The function strftime('%d-%m-%Y',date('now')) returns a string with the month in numeric format (from 01 to 12). As far as I can tell from the docs, there is no format specifier to return the full name of the month.
I think you'll have to store your dates using numerical month specifiers instead of names.