user2549221
user2549221

Reputation: 209

sqlite date query is not working well in android

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

Upvotes: 0

Views: 361

Answers (1)

user2549221
user2549221

Reputation: 209

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

Upvotes: 2

Related Questions