I need to perform a SELECT queries that are insensitive to case and accents. For demo purposes, I create a table like that:
create table table
(
column text collate nocase
);
insert into table values ('A');
insert into table values ('a');
insert into table values ('Á');
insert into table values ('á');
create index table_cloumn_Index
on table (column collate nocase);
Then, I get those results when executing the following queries:
SELECT * FROM table WHERE column LIKE 'a';
> A
> a
SELECT * FROM table WHERE column LIKE 'á';
> á
SELECT * FROM table WHERE column LIKE 'Á';
> Á
How can I fix that so the results for any of the following queries be like that:
> A
> a
> Á
> á
The sqlite is running on iOS, by the way.
Thanks in advance,
Here is my solution of LIKE problem
And then instead of query
you should use
Two basic approaches:
You can create a second column in the table which contains the string without the international characters. Furthermore, before doing a search against this secondary search column, you should also remove international characters from the string being search for, too (that way you are comparing non-international to non-international).
This is the routine I use to convert the international characters:
You could also replace the accented characters with:
By the way, if you need to sort your results, you can also sort upon this secondary search field instead of the main field, which will avoid problems stemming from SQLite's inability to sort the international characters, either.
You can alternatively create your own "unaccented" C function (define this C function outside the
@implementation
for your class):You can then define a SQLite function that will call this C-function (call this method after you open the database, which will be effective until you close that database):
Having done that, you can now use this new
unaccented
function in SQL, e.g.:You will need to either create some user function, or override (i.e. replace) the default implementation of the
like()
functions. The reason is that theLIKE
operator in sqlite doesn't support non-ASCII case-insensitiveness:This makes sense otherwise sqlite would need to support different cultures since case varies from one to the other. An example is the capital
i
in Turkey which is notI
but a dottedİ
, and the lower-case ofI
is a dot-lessı
. Embedding all this culture information in sqlite would be very burdensome (i.e. it would increase sqlite object code).