Created
February 3, 2020 15:20
-
-
Save vehrka/9a9ec4aecd7092a55084bbdea5394416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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