how to compare dates .
for example my date format is 2011/10/12(YYYY/MM/DD).
I'm having some date records in database .i want some record greater than particular date.
for example :
String compareDate=2011/10/16;
select * from testtable where date>compareDate;
how to get least date and highest date through database query.
in sqlite is there any method to compare dates.
Thanks in Advance
Push the date to query in right format
select * from testtable where date > '2011-10-16';
more
Mysql Compare two datetime fields
http://dev.mysql.com/doc/refman/5.0/en/using-date.html
http://www.java2s.com/Code/SQL/Date-Time/Comparedateinwhereclause.htm
Before going to execute query, compareDate should be in this ("yyyy-MM-dd") format,
then try this query
select date from testtable where date > 'compareDate' order by date desc;
the above query order the date by descending order. The first row of cursor will have the max date.
Related
I have a table in SQLite that has three fields (id, fromDate, toDate, uid). I want to select rows that has created in a specific dates. User select the period of time (from date/ to date). How I can query with ORMLite?
Sqlite does not have a Date type(supported types)
There is some dateTime functions, i think that any of function will meet your requirement... The easier solution will be to convert you date to a timestamp and compare the timestamp.
I'm using a sqlie database wich have a datetime column, i stored values of the datetime column with format "yyyy-MM-dd HH:mm" but when i want to select values from Database and order by the datetime column, it gives me a wrong order.
SELECT id_user FROM RDV WHERE id_user= ? ORDER BY datetime(Date) DESC
i added datetime(Date) just to check if it works but it didn't work also
Acually i made in error, tha dates where in format "dd-MM-yyyy HH:mm" and i must put them in format yyyy-MM-dd HH:mm
What is the column name?
If it's called Date, just remove the call to datetime():
order by Date desc (or the name of the field if different)
I am storing date in ISO8601 format example 2015-04-15T10:54:14Z in sqlite table, I want youngest date from table. below are the dates in my sqlite table
2015-04-15T10:54:14Z
2015-04-15T10:54:115Z
2015-04-15T10:54:216Z
2015-04-15T10:54:320Z
2015-04-15T10:54:422Z
I am trying below query:
SELECT * FROM Table1 ORDER BY datetime("date_column") DESC ;
but I am not getting appropriate result.
ISO 8601 datetime stamps normalized to UTC have the nice property that the alphabetical (lexicographic) order is also temporal order.
You don't need the datetime(), you can just ORDER BY date_column DESC to sort them newest first, and you can add LIMIT 1 to get just the newest one.
Update your query to this:
SELECT * FROM Table1 ORDER BY date_column DESC LIMIT 1
use this method to put date column in your db:
public String getDatetime(){
//get time and date
Calendar c=Calendar.getInstance();
CharSequence s = DateFormat.format("yyyy-MM-dd HH:mm:ss", c.getTime());
//convert it to string array
return s.toString();
}
then use your query as you used before, cause datetime() accepts specific formatts.
I am trying to retrieve data from the database with the following query:
select * from Oil Where date(DDate)=date('2012-08-07');
where DDate is one of the column in the Oil table with datatype text.I get the Arrayindexoutofboundsexception. I found that it is not returning any value the count is 0.
can anyone help me with this.What i am actually trying is to do is store date value in the table.I couldn't find which datatype should be used to save date,from the sqlite site is that it doesn't have a particular datatype for date and time.
SQL Server comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MM:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS
select * from Oil Where date(DDate)=date('2012-08-07');
can be
select * from Oil Where DDate='2012-08-07';
If you have DDate in the date format 'yyyy-MM-dd' then you can directly compare the date using
select * from Oil Where DDate='2012-08-07';
else if you have DDate in the date format 'yyyy-MM-dd HH:mm:ss' then the query looks like this
select * from Oil Where date(DDate)='2012-08-07';
If you are saving date in sqlite database and you have to perform date operations like sorting or using date() and time() functions then you have to use the 'yyyy-MM-dd HH:mm:ss' date format.
Datatype doesn't matter for sqlite.
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?