boobalan gnanasekaran boobalan gnanasekaran - 23 days ago 6
SQL Question

Compare Strings using SQL and use it as an IF Condition to execute an SQL Query

I have two Strings in Java.

String from = "string1";
String upto = "string2";


Now I compare these Strings the way Java does and do the following

Something like this,

Cursor cursor;
if(from.compareTo(upto) > 0){
cursor = database.execSql("SELECT * FROM table WHERE name BETWEEN '" + from + "' AND '"+ upto+"' OR b.name GLOB '"+upto+"*');
} else {
cursor = database.execSql("SELECT * FROM table WHERE name BETWEEN '" + from + "' AND '"+ upto+"');
}


But I desire to compare the Strings from and upto in the way Sql does unlike how Java does and use the appropriate query.

Does anyone have idea on this?
Or, is there a way to construct a single sql query to do that?

Answer
SELECT *
FROM
    table
WHERE
    name BETWEEN from AND upto
    OR (from = upto
       AND b.name GLOB upto)

Your question is a little cryptic so I am not positive I fully understand it but it seems to merge your statements you just need to add the or and as part of that add from = upto and your other condition.

I will assume you can add the appropriate tick marks etc to build this in your code. hope it gives you an idea of how to do this.

Comments