PostgreSQL – Function to remove accents in string

Table of Contents

PostgreSQL - Function to remove accents in string

Loại bỏ dấu câu, chuyển câu có dấu thành không dấu trong PostgreSQL

Ex: José, Jose, Jósé, Jóse -> Jose

Load extension unaccent

CREATE EXTENSION unaccent;

image

Run SQL to remove accents in string

Ví dụ này tôi chuyển các họ thành họ không dấu

SELECT t1.ho as ho, unaccent(t1.ho) as no_accent FROM ds_ho t1 LIMIT 10;

image

References

https://stackoverflow.com/questions/13596638/function-to-remove-accents-in-postgresql

Use the unaccent module that ships with PostgreSQL.

somedb=# CREATE EXTENSION unaccent;
CREATE EXTENSION
somedb=# SELECT unaccent('Hôtel');
 unaccent
----------
 Hotel

somedb=# SELECT * FROM table WHERE lower(unaccent(table.id)) = lower('Jose');

And speed things up a bit by generating an accentless, lower-cased index:

somedb=# CREATE INDEX CONCURRENTLY ON table (lower(unaccent(id)));

Leave a Reply

Your email address will not be published. Required fields are marked *