Skip to content

Instantly share code, notes, and snippets.

@vehrka
Created February 3, 2020 15:20
Show Gist options
  • Save vehrka/9a9ec4aecd7092a55084bbdea5394416 to your computer and use it in GitHub Desktop.
Save vehrka/9a9ec4aecd7092a55084bbdea5394416 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION slugify("value" TEXT)
RETURNS TEXT AS $$
WITH lowercased AS (
SELECT lower(value) AS value
),
translated AS (
SELECT translate( value,
'áàâãäåāăąèééêëēĕėęěìíîïìĩīĭḩóôõöōŏőùúûüũūŭůäàáâãåæçćĉčöòóôõøüùúûßéèêëýñîìíïş',
'aaaaaaaaaeeeeeeeeeeiiiiiiiihooooooouuuuuuuuaaaaaaeccccoooooouuuuseeeeyniiiis'
) AS value
FROM lowercased
),
regexped AS (
SELECT regexp_replace( value, '[^a-z0-9]+', ' ', 'g') AS value
FROM translated
),
trimmed AS (
SELECT TRIM( value ) AS value
FROM regexped
),
replaced AS (
SELECT REPLACE( value ,' ', '_') AS value
FROM trimmed
)
SELECT value FROM replaced;
$$ LANGUAGE SQL STRICT IMMUTABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment