John Smith
John Smith

Reputation: 752

Android SQLite LOCATE

Ok, I realize now that my question was too short or something, so I will try my best.

The structured query language of MySQL has a function

LOCATE(substr,str) 

that enables one to restrict a searchquery to a substring that only occures at a certain position, for instance the beginning of the result string. An example would be:

SELECT * FROM `mytable` 
WHERE REPLACE('searchstring', `mytable`.`columnname`, '') != 'searchstring' 
AND LOCATE(`mytable`.`columnname`, 'searchstring') = 1;

By doing so, I tell MySQL to only query a result where the substring I am searching occures at the beginning of said string. I was wondering, how might one achive this using Android SQLite, since Android SQLite doesn't seem to support LOCATE. I also had a look at POSITION, which is also not available on Android.

Upvotes: 0

Views: 938

Answers (1)

MPelletier
MPelletier

Reputation: 16667

I'm afraid there is no direct SQLite equivalent for LOCATE. However, you can get the equivalent of your filter with a LIKE:

SELECT * FROM mytable
WHERE mytable.columnname LIKE 'searchstring' || '%';

Here I use concatenation (||) to append the wildcard %, so that you can use a variable to put in place of searchstring and not worry about formatting it every time to include the wildcard.

Upvotes: 1

Related Questions