语法错误是由于 sql 语法和不正确的返回值造成的:
CREATE FUNCTION myFunction(str text) RETURNS text AS $$
SELECT LOWER(replace(replace(replace((str, 'ü', 'ue'), 'ä', 'ae'), 'ö', 'oe'));
$$ IMMUTABLE STRICT LANGUAGE sql;
替代,更完整的功能:
CREATE OR REPLACE FUNCTION translit(text)
RETURNS text
AS $$
DECLARE
_str text := $1;
_find constant text := 'ÀÁÂÃÄÅĀĄĂÆÈÉÊËĒĘĚĔĖÐÌÍÎÏĪĨĬĮİIJÒÓÔÕÖØŌŐŎŒÙÚÛÜŪŮŰŬŨŲÝŶŸ' ||
'àáâãäåāąăæèéêëēęěĕėðìíîïīĩĭįıijòóôõöøōőŏœùúûüūůűŭũųýÿŷ' ||
'ÇĆČĈĊĎĐƑĜĞĠĢĤĦĴĶŁĽĹĻĿÑŃŇŅŊŔŘŖŚŠŞŜȘſŤŢŦȚÞŴŹŽŻ' ||
'çćčĉċďđƒĝğġģĥħĵĸķłľĺļŀñńňņʼnŋŕřŗśšşŝșßťţŧțþŵžżź';
_repl constant text := 'AAAAAAAAAAEEEEEEEEEEIIIIIIIIIIOOOOOOOOOOUUUUUUUUUUYYY' ||
'aaaaaaaaaaeeeeeeeeeeiiiiiiiiiioooooooooouuuuuuuuuuyyy' ||
'CCCCCDDFGGGGHHJKLLLLLNNNNNRRRSSSSSSTTTTTWZZZ' ||
'cccccddfgggghhjkklllllnnnnnnrrrsssssstttttwzzz';
BEGIN
-- Bail if the string is empty.
IF _str = ''
THEN
RETURN _str;
END IF;
-- Common expansions.
_str := replace(_str, 'ſ', 'SS');
_str := replace(_str, 'ß', 'ss');
_str := replace(_str, 'Å', 'AA');
_str := replace(_str, 'å', 'aa');
_str := replace(_str, 'IJ', 'IJ');
_str := replace(_str, 'ij', 'ij');
_str := regexp_replace(_str, '([ÄÆÖŒÜ])', E'\\1E', 'g');
_str := regexp_replace(_str, '([äæöœü])', E'\\1e', 'g');
-- Translate what's left.
_str := translate(_str, _find, _repl);
RETURN _str;
END;
$$ IMMUTABLE STRICT LANGUAGE plpgsql COST 1;