Ignore special characters in the WHERE clause

3.1k Views Asked by At

I have a table named artists with a record with the value 'Miró' in the name column. When I do this request:

SELECT "artists".* FROM "artists" WHERE name = 'Miró'

I have one result, so it works.

Now, when I do this request (without the special ó) :

SELECT "artists".* FROM "artists" WHERE name = 'Miro'

I don't find anything. I want to ignore the special char. Is there a way to do it?
I have postgres 9.1.9.

2

There are 2 best solutions below

0
On BEST ANSWER

For a more targeted pattern matching, you can use the function unaccent(), provided by the additional module unaccent:

SELECT * FROM artists WHERE unaccent(name) = 'Miro';

To make this fast, create a functional index. You have to overcome the obstacle that the function is only STABLE, not IMMUTABLE. I wrote a comprehensive answer with instructions (including installation) and links recently:
Does PostgreSQL support "accent insensitive" collations?

2
On

You could try using LIKE instead...

SELECT "artists".* FROM "artists" WHERE name like 'Mir%'