Skip to content

Instantly share code, notes, and snippets.

@NielsLiisberg
Last active November 11, 2021 14:57
Show Gist options
  • Save NielsLiisberg/dfa78c127a5863d99593cda86d458353 to your computer and use it in GitHub Desktop.
Save NielsLiisberg/dfa78c127a5863d99593cda86d458353 to your computer and use it in GitHub Desktop.
-- UDTF to return a snake-case of a string for column naming purposes
-- Simply paste this gist into ACS SQL and run it to create the UDTF.
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2021
-- This gist is distributed on an "as is" basis, without warranties
-- or conditions of any kind, either express or implied.
----------------------------------------------------------------------------------------------
create or replace function qusrsys.snake_case (
name varchar(128)
)
returns varchar(128)
no external action
set option output=*print, commit=*none, dbgview = *list
begin
declare temp varchar(128);
set temp = lower(name);
set temp = regexp_replace(temp , '[(].*[)]' , '');
set temp = regexp_replace(temp , '[æ]' , 'ae');
set temp = regexp_replace(temp , '[ø@]' , 'oe');
set temp = regexp_replace(temp , '[å]' , 'aa');
set temp = regexp_replace(temp , '[^a-z0-9]' , ' ');
set temp = trim(temp); -- Done here if the name has trailing invalid chars
set temp = regexp_replace(temp , ' +', '_');
-- Columns names can not begin with a digit
if substring(temp , 1 , 1) >= '0' and substring(temp , 1 , 1) <= '9' then
set temp = 'x' concat temp;
end if;
return temp;
end;
-- usecase
values ( qusrsys.snake_case('Saldo beløb'));
values ( qusrsys.snake_case('Saldo ( men uden det her) beløb '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment