I want to compare current month data(expenses) with previous month data(expenses) which is store in database datewise. kindly some one guide me please.
I write this command for current month data but did not work for me.
Cursor
cursor= database.rawQuery("
SELECT amount
FROM Groceryitems
WHERE Strftime('%Y-%m', 'now')
", new String[]{});
There must be a date column in the table Groceryitems and this column you must check if it contains a date of the current month:
String sql = "SELECT amount FROM Groceryitems WHERE strftime('%Y-%m', datecolumn) = strftime('%Y-%m', 'now')";
cursor= database.rawQuery(sql, null);
Change datecolumn with the name of the date column in the table.
To get the previous month's data:
String sql = "SELECT amount FROM Groceryitems WHERE strftime('%Y-%m', datecolumn) = strftime('%Y-%m', 'now', '-1 month')";
cursor= database.rawQuery(sql, null);
Related
I have a query which should return total Income - total expense for the given date
db.rawQuery("select (totIncome-totExpense) from " +
"((select coalesce(sum(amount),0) as totIncome from transaction_table where type=0 and date like '"+ ke+"'), " +
"(select COALESCE(sum(amount),0) as totExpense from transaction_table where type=1 and date like '"+ ke+"'))",null);
But when there are no records for type=0, for a given date, the query returns an empty cursor instead of -totExpense.
You can do conditional aggregation with sum(case...):
db.rawQuery(
"select coalesce(sum(case type when 0 then amount else 0 end) - sum(case type when 1 then amount else 0 end), 0) as result from transaction_table where date like ?",
new String[] {ke}
);
This way the sums are calculated with only one scan of the table.
The function coalesce() is needed only for the case that there are no rows in the table for that date.
I also took out from the query the date ke and passed it is a parameter, which is safer.
Maybe instead of like you can use = if ke is just a date and does not contain wildchars like % or _.
I have table in database where one of column is for date in Long. Is there any simple way to get all dates by month? For example I need all dates for October. For day I use:
public static List<Plan> getPlanListByMonth(SQLiteDatabase db, DateTime date) {
String where = "date" + " = '"+ "2015-11-04" +"'";
Cursor cursor = db.query(TABLE_NAME, null, where,
null, null, null, null, null);
return getPlanList(cursor);
}
but I have no idea how to create where condition to get all values by month.
You can use strftime() function
SELECT * FROM TABLE_NAME WHERE strftime('%m', 'date') = '11'
I have dates stored in a SQLite table in int format (i.e. milliseconds - derived from System.currentTimeMillis()). I now want to query all rows from the table where the date is equal to today's date, but the query below always returns zero, even though file_uploaded_date in the upload_history table is set to today's date in at least 20 rows.
Can anyone tell me what's wrong with my query?
String today = new SimpleDateFormat("d-MMMM-yyyy").format(new Date());
String sql = "SELECT COUNT(*) as uploaded_today from upload_history "
+ "WHERE strftime('%-d-%b-%Y',file_uploaded_date) = strftime('%-d-%b-%Y','" + today + "')";
Cursor cursor = db.rawQuery(sql, null);
if(cursor != null && cursor.moveToFirst()){
int uploadedToday = cursor.getInt(cursor.getColumnIndex("uploaded_today"));
}
I'd say you have your format-strings incorrect.
I don't see a %b argument in the documentation. For month, you would want to use %m. Also %-d doesn't seem right. Use the following format string instead: %Y%m%d.
Further, you are then passing a poorly-formatted string into the query, rather than the int, and relying an sqlite to correct that. Instead, compare to a SimpleDateFormat( "yyyyMMdd" ) without further conversion.
Your code would then look like this:
String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
String sql = "SELECT COUNT(*) from upload_history "
+ "WHERE strftime('%Y%m%d',file_uploaded_date) = '" + today + "')";
Cursor cursor = db.rawQuery(sql, null);
if(cursor != null && cursor.moveToFirst()){
int uploadedToday = cursor.getInt(0);
}
(Note that if you return only one column, you don't have to name it, and can just access the first column for the result.)
Further, please be aware that this query will cause a table-scan every time it's executed, as sqlite needs to convert all the epochs to strings. You'd be better off adding a date column to the table, and update it once:
UPDATE upload_history
SET just_the_date = strftime('%Y%m%d',file_uploaded_date)
This will then allow you to do much quicker searches, and even search by year or month, using the LIKE operator. If your table is very large, you might want to put an index on that column as well.
You can add date in db as a string in date format like yyyy-mm-dd hh-mm-ss and compare the same while retrieving it from database using sql query.
I am having below table on my android sqlite database.
My Date filed is TEXT;
ID Name Date
1 ABY 2014-12-01
2 RUBY 2015-01-10
3 AMY 2015-01-15
4 ROBEN 2014-10-25
I need to sort the result like in mysql
select * from Table where YEAR(DATE)='2015';
How can I get the above result in Andorid sqlite database?
I think you want to use the LIKE operand
SELECT * FROM table WHERE Date LIKE '2015%'
http://www.tutorialspoint.com/sqlite/sqlite_like_clause.htm
Edit for query:
String selectQuery = "SELECT id, name FROM table WHERE date like \'" + "2015" + "\'";
Cursor c = db.rawQuery(selectQuery, new String[] { fileNameOfDb });
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex("name"));
}
c.close();
I store date as TEXT in my database in this format YYYY-MM-DD.
Each row has start date and final date.
I want to select the row that today date is between the start date and final date.
Today date is string and is in the same format as those ex.2014-07-29
I've tried
SELECT * FROM TABLE_NAME WHERE date(todayDate) BETWEEN date(COLUMN_START_DATE) AND date(COLUMN_FINAL_DATE)
but it didn't work. I get 0 row.
I've also tried
SELECT * FROM TABLE_NAME WHERE strftime('%Y-%m-%d', todayDate) BETWEEN COLUMN_START_DATE AND COLUMN_FINAL_DATE
Still not work.
What am I doing wrong?
use this to get the results:-
String selection = "from_duration<=Datetime('" + initial_date
+ "') AND to_duration>=Datetime('" + final_date + "')";
your_database.query(Table_Name,Columns_youwant, selection,
null, null, null, null);