Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to remove accents in postgreSQL [duplicate]

Possible Duplicate:
Does PostgreSQL support “accent insensitive” collations?

I'm trying to remove the accents so when I make a SELECT it ignores them.

Example:

SELECT * FROM table WHERE table.id ILIKE 'Jose'; 

It returns:

José Jose Jósé Jóse 

or something like that.

I found these functions and they wont work, I'm thinking it may be the fact that I'm using Struts 1.X, please check them out and tell me where I'm wrong or what other function should I use.

FIRST FUNCTION

CREATE OR REPLACE FUNCTION unaccent_string(text) RETURNS text AS $$ DECLARE     input_string text := $1; BEGIN  input_string := translate(input_string, 'âãäåāăąÁÂÃÄÅĀĂĄ', 'aaaaaaaaaaaaaaa'); input_string := translate(input_string, 'èééêëēĕėęěĒĔĖĘĚ', 'eeeeeeeeeeeeeee'); input_string := translate(input_string, 'ìíîïìĩīĭÌÍÎÏÌĨĪĬ', 'iiiiiiiiiiiiiiii'); input_string := translate(input_string, 'óôõöōŏőÒÓÔÕÖŌŎŐ', 'ooooooooooooooo'); input_string := translate(input_string, 'ùúûüũūŭůÙÚÛÜŨŪŬŮ', 'uuuuuuuuuuuuuuuu');  return input_string; END; $$ LANGUAGE plpgql; 

SECOND FUNCTION

CREATE OR REPLACE FUNCTION norm_text_latin(character varying)    RETURNS character varying AS  $BODY$  declare          p_str    alias for $1;          v_str    varchar;  begin          select translate(p_str, 'ÀÁÂÃÄÅ', 'AAAAAA') into v_str;          select translate(v_str, 'ÉÈËÊ', 'EEEE') into v_str;          select translate(v_str, 'ÌÍÎÏ', 'IIII') into v_str;          select translate(v_str, 'ÌÍÎÏ', 'IIII') into v_str;          select translate(v_str, 'ÒÓÔÕÖ', 'OOOOO') into v_str;          select translate(v_str, 'ÙÚÛÜ', 'UUUU') into v_str;          select translate(v_str, 'àáâãäå', 'aaaaaa') into v_str;          select translate(v_str, 'èéêë', 'eeee') into v_str;          select translate(v_str, 'ìíîï', 'iiii') into v_str;          select translate(v_str, 'òóôõö', 'ooooo') into v_str;          select translate(v_str, 'ùúûü', 'uuuu') into v_str;          select translate(v_str, 'Çç', 'Cc') into v_str;          return v_str;  end;$BODY$    LANGUAGE 'plpgsql' VOLATILE;  

Both functions generate the following error when I run the file in NetBeans:

ERROR: unterminated dollar-quoted string at or near "*the string that starts the function*" 
like image 850
chamini2 Avatar asked Nov 28 '12 02:11

chamini2


People also ask

How do I get rid of accents in postgresql?

The unaccent() function removes accents (diacritic signs) from a given string.

How do I remove a character from a string in postgresql?

The LTRIM() function removes all characters, spaces by default, from the beginning of a string. The RTRIM() function removes all characters, spaces by default, from the end of a string. The BTRIM function is the combination of the LTRIM() and RTRIM() functions.

What is collation in Postgres?

The collation feature allows specifying the sort order and character classification behavior of data per-column, or even per-operation. This alleviates the restriction that the LC_COLLATE and LC_CTYPE settings of a database cannot be changed after its creation.


1 Answers

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))); 
like image 104
titanofold Avatar answered Sep 21 '22 12:09

titanofold