Skip to content

Instantly share code, notes, and snippets.

@undernewmanagement
Forked from chanmix51/transliterate.sql
Created April 10, 2012 04:46
Show Gist options
  • Save undernewmanagement/2348365 to your computer and use it in GitHub Desktop.
Save undernewmanagement/2348365 to your computer and use it in GitHub Desktop.
transliteration in plpgsql
CREATE OR REPLACE FUNCTION transliterate(my_text VARCHAR) RETURNS varchar AS $$
DECLARE
text_out VARCHAR DEFAULT '';
BEGIN
text_out := my_text;
text_out := translate(text_out, 'àâäåáăąãā', 'aaaaaaaaa');
text_out := replace(text_out, 'æ', 'ae');
text_out := translate(text_out, 'çċćčĉ', 'ccccc');
text_out := translate(text_out, 'éèėëêēĕ', 'eeeeeee');
text_out := translate(text_out, 'îïìíī', 'iiiii');
text_out := translate(text_out, 'ñ', 'n');
text_out := translate(text_out, 'ôöøõō', 'ooooo');
text_out := replace(text_out, 'œ', 'oe');
text_out := translate(text_out, 'ùúüûū', 'uuuuu');
text_out := translate(text_out, 'ýÿỳ', 'yyy');
text_out := replace(text_out, 'ß', 'ss');
text_out := translate(text_out, 'ÀÂÄÅÁĂĄÃĀ', 'AAAAAAAAA');
text_out := replace(text_out, 'Æ', 'AE');
text_out := translate(text_out, 'ÇĊĆČĈ', 'CCCCC');
text_out := translate(text_out, 'ÉÈĖËÊĒĔ', 'EEEEEEE');
text_out := translate(text_out, 'ÎÏÌÍĪ', 'IIIII');
text_out := translate(text_out, 'Ñ', 'N');
text_out := translate(text_out, 'ÔÖØÕŌ', 'OOOOO');
text_out := replace(text_out, 'Œ', 'OE');
text_out := translate(text_out, 'ÙÚÜÛŪ', 'UUUUU');
text_out := translate(text_out, 'ÝŸỲ', 'YYY');
RETURN text_out;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment