Reputation: 87
Hi everyone I have to query from db using BETWEEN on two DOUBLE values
This is my Table:
db.execSQL("CREATE TABLE Task ("
+ "id smallint(6) NOT NULL,"
+ "project_id smallint(6) NOT NULL,"
+ "add_date varchar(15) NOT NULL,"
+ "geo_lat double NOT NULL,"
+ "geo_long double NOT NULL,"
+ "additional_info TEXT NULL" + ");");
I want to show all nearest Tasks and my current QUERY is:
SELECT * FROM Task WHERE geo_lat BETWEEN " + lat_top + " AND " + lat_bot + " AND geo_long" + " BETWEEN " + long_top + " AND " + long_bot
I dont know is it wrong or not, i have found it somewhere in this forum. This query returns me nothing... How it shoud be done?
Upvotes: 1
Views: 837
Reputation: 9904
Just change the order of the top and bottom in your query:
SELECT * FROM Task WHERE geo_lat
BETWEEN " + lat_bot+ " AND " + lat_top + "
AND geo_long" + "
BETWEEN " + long_bot + " AND " + long_top
Your sample filled query would be like:
SELECT * FROM TASK WHERE
( GEO_LAT BETWEEN 12.01 AND 12.50 )
AND
(GEO_LONG BETWEEN 81.10 AND 91.20)
Upvotes: 1
Reputation: 180080
a BETWEEN b AND c
is the same as a >= b AND a <= c
.
So you have to ensure that the smaller of the two values comes before the AND
.
Upvotes: 3