Last active
August 24, 2021 08:33
-
-
Save NielsLiisberg/f3be70d38b8a053bc61ee9987f03887a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-- UDTF to return a camelcase 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.camel_case ( | |
name varchar(128) | |
) | |
returns varchar(128) | |
no external action | |
set option output=*print, commit=*none, dbgview = *list | |
begin | |
declare temp varchar(128); | |
declare outString varchar(128); | |
declare i int; | |
declare upperNext int; | |
declare c char(1); | |
-- First snake-case the name to sanitise it AND it will also works if the string is already snake-case | |
set temp = lower(name); | |
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' then | |
set temp = 'x' concat temp; | |
end if; | |
set i = 1; | |
set upperNext = 0; | |
set outString = ''; | |
while i <= length(temp) do | |
set c = substr(temp , i ,1); | |
if c = '_' then | |
set upperNext = 1; | |
elseif upperNext = 1 then | |
set outString = outString || upper(c); | |
set upperNext = 0; | |
else | |
set outString = outString || c; | |
end if; | |
set i = i +1; | |
end while; | |
return outString; | |
end; | |
-- usecase | |
values ( qusrsys.camel_case('Saldo beløb')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment