Re: how to ignore accents?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: nasr(dot)laili(at)tin(dot)it, pgsql-novice(at)postgresql(dot)org
Subject: Re: how to ignore accents?
Date: 2005-04-02 01:41:14
Message-ID: 20050402014113.GA9031@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Apr 01, 2005 at 08:36:35PM +0200, Ennio-Sr wrote:
>
> CREATE FUNCTION unaccent(text) RETURNS text AS '
> BEGIN
> RETURN translate(&parola, "\342\347\350\351\352\364\373", "aceeeou");
> ^^^^^^^
> END;
> ' LANGUAGE plpgsql IMMUTABLE STRICT;
>
> ^^^^ [I also tried with field, &field, parola, (parola), [parameter
> "&field?"], $1, arg1 and similar]

See the PL/pgSQL documentation to get a better idea of the syntax:

http://www.postgresql.org/docs/7.4/interactive/plpgsql.html

Let's get the basic functionality working before messing with
accented characters. Try this:

CREATE FUNCTION unaccent(text) RETURNS text AS '
BEGIN
RETURN translate($1, ''ABC'', ''abc'');
END;
' LANGUAGE plpgsql IMMUTABLE STRICT;

CREATE TABLE parole (
id serial PRIMARY KEY,
parola text NOT NULL
);

INSERT INTO parole (parola) VALUES ('AAA');
INSERT INTO parole (parola) VALUES ('BBB');
INSERT INTO parole (parola) VALUES ('CCC');

SELECT unaccent(parola) FROM parole;
unaccent
----------
aaa
bbb
ccc
(3 rows)

When you get that working, then you can modify the translate()
strings to convert accented characters to their unaccented
equivalents.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Morgan Kita 2005-04-02 03:45:35 Major problem with custom data type
Previous Message Michael Fuhr 2005-04-02 00:38:10 Re: plpgsql question - simple I would have thought