可能重复:
Does PostgreSQL support “accent insensitive” collations?
我正在尝试删除重音符,所以当我创建一个 SELECT 时,它会忽略它们。
示例:
SELECT * FROM table WHERE
table.id ILIKE 'Jose';
它返回:
José
Jose
Jósé
Jóse
或类似的东西。
我发现这些功能并且它们不起作用,我想我可能是因为我正在使用Struts 1.X,请检查它们并告诉我哪里错了或我应该使用哪些其他功能
第一个功能
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;
第二功能
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;
在NetBeans中运行文件时,这两个函数都会生成以下错误:
ERROR: unterminated dollar-quoted string at or near "*the string that starts the function*"
答案 0 :(得分:74)
使用PostgreSQL附带的unaccent模块。
somedb=# CREATE EXTENSION unaccent;
CREATE EXTENSION
somedb=# SELECT unaccent('Hôtel');
unaccent
----------
Hotel
somedb=# SELECT * FROM table WHERE lower(unaccent(table.id)) = lower('Jose');
通过生成无重复,低位的索引来加快速度:
somedb=# CREATE INDEX CONCURRENTLY ON table (lower(unaccent(id)));