How to ignore accents in an SQL query?

7

How could I perform a query by ignoring words with letters that contain accents? Word Example:

Olá, é, Lógica, Pão

I tried to use the collate noaccents command as follows:

select c.* from Curso c 
where c.Descricao like '%Logica%' collate noaccents;

However, I did not get any results, and only works if I remove the accent from the word Lógica . So how could I ignore the accents?

Minimum example

Commands to create the illustration example.

Table :

create table Curso(Descricao TEXT);

Insert :

insert into Curso(Descricao) values ('Lógica de Programação');

See working in SQL Fiddle .

    
asked by anonymous 25.07.2018 / 06:02

2 answers

2
  • I would go this way: How to make case-insensitive comparisons in SQLite? . It's the simple .

  • Note that some SQLite implementations already have a way to handle this, but they no longer have the greater advantage of SQLite, which is to be a small code. It has the advantage of dealing with more sophisticated cases. But it will be slower. The question does not give a context of use. I talked about this in SQLite android query () / like with accents and without .

    If your implementation does not have something ready you can put a complete way of dealing with it. Has the same or more difficulty than the previous one. In practice you need to know more what you are doing. You can even make an implementation that stays the same size as SQLite customizing the ICU . It will be very limited, even more than the previous solution, even though it is much larger.

  • The alternative is to create a column without the accents and index it and use it for any search (after taking the accents and making everything tiny), and at the time that pick up the data itself use the column with the original data. This takes up much more memory and makes everything much slower, but it works. It is gambiarra, solution of who does not want to do the right.

  • Another solution is to drop accents once and only work with ASCII. I think that's terrible from the point of view of UX, and a lot of the 80's. But I see systems working like this until today. It's backsliding.

SQLite is an excellent database, simple to use on a daily basis, and performatic, but it is not something ready, it needs some effort to do beyond the basics.

    
25.07.2018 / 15:22
0

Just set up a Collation using sqlite3_create_collation and use it as follows:

SELECT * FROM Curso WHERE Descricao LIKE "Logica%" COLLATE NOACCENTS 
    
25.07.2018 / 13:06