snake_case → PascalCase
SELECT REPLACE(INITCAP('snake_case_string'), '_', '')
DROP FUNCTION IF EXISTS SNAKE_TO_PASCAL;
CREATE FUNCTION SNAKE_TO_PASCAL(str TEXT)
RETURNS TEXT AS $$
BEGIN
RETURN REPLACE(INITCAP(str), '_', '');
END; $$
LANGUAGE plpgsql;
snake_case → camelCase
SELECT LOWER(LEFT(REPLACE(INITCAP('snake_case'), '_', ''), 1)) || RIGHT(REPLACE(INITCAP('snake_case'), '_', ''), -1)
DROP FUNCTION IF EXISTS SNAKE_TO_CAMEL;
CREATE FUNCTION SNAKE_TO_CAMEL(str TEXT)
RETURNS TEXT AS $$
BEGIN
str := REPLACE(INITCAP(str), '_', '');
RETURN LOWER(LEFT(str, 1)) || RIGHT(str, -1);
END; $$
LANGUAGE plpgsql;
camelCase → snake_case
SELECT LOWER(REGEXP_REPLACE('camelCaseString', '([A-Z])', '_\\1', 'g'))
DROP FUNCTION IF EXISTS CAMEL_TO_SNAKE;
CREATE FUNCTION CAMEL_TO_SNAKE(str TEXT)
RETURNS TEXT AS $$
BEGIN
RETURN LOWER(REGEXP_REPLACE(str, '([A-Z])', '_\\1', 'g'));
END; $$
LANGUAGE plpgsql;
PascalCase → snake_case
SELECT LOWER(REGEXP_REPLACE(LOWER(LEFT('PascalCase', 1)) || RIGHT('PascalCase', -1), '([A-Z])', '_\\1', 'g'))
DROP FUNCTION IF EXISTS PASCAL_TO_SNAKE;
CREATE FUNCTION PASCAL_TO_SNAKE(str TEXT)
RETURNS TEXT AS $$
BEGIN
RETURN LOWER(REGEXP_REPLACE(LOWER(LEFT('PascalCase', 1))
|| RIGHT('PascalCase', -1), '([A-Z])', '_\\1', 'g'));
END; $$
LANGUAGE plpgsql;