create or replace FUNCTION "GEN1_9A_Z" ( start_val varchar2)
return varchar2
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-- GEN1_9A_Z generate next unique alphanumeric string with size 3 -- (0..9A..Z) --
-- Return 0 when error or end (ZZZ). --
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-- 001,002,..,009,00A,00B,..,00Z,
-- 010,011,..,019,01A,01B,..,01Z,
-- .............................
-- 090,091,..,099,09A,09B,..,09Z,
-- 0A0,0A1,.................,0AZ,
-- 0B0,0B1,.................,0BZ,
-- .............................
-- 0Z0,0Z1,.................,0ZZ,
-- 100,101,.................,10Z,
-- 110,111,.................,11Z,
-- .............................
-- 990,991,.................,99Z,
-- 9A0,9A1,.................,9AZ,
-- .............................
-- 9Z0,9Z1,.................,9ZZ,
-- A00,A01,.................,A0Z,
-- A10,A11,.................,A1Z,
-- .............................
-- A90,A91,.................,A9Z,
-- AA0,AA1,.................,AAZ,
-- AB0,AB1,.................,ABZ,
-- ..............................
-- AZ0,AZ1,.................,AZZ,
-- B00,B01,.................,B0Z,
-- ..............................
-- ..............................
-- ZZ0,ZZ1,.................,ZZZ ---- END!!!
------------------------- generate 46656 unique alphanumeric. ( 000 - ZZZ ) --------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
is
start_value varchar2(3);
return_val varchar2(3);
begin
if length(start_val) <= 3 then
start_value := lpad(upper(start_val),3,'0');
select
(case
when p1= 0 and p2 = 0 and p3 = 0 and v3 != '9' then lpad(to_char(start_value+1),3,'0')
when p1= 0 and p2 = 0 and p3 = 0 and v3 = '9' then v1||v2||chr(65)
when p1= 0 and p2 = 0 and p3 = 1 and ASCII(v3) between 65 and 89 then v1||v2||chr(ASCII(v3)+1)
when p1= 0 and p2 = 0 and p3 = 1 and v2 != '9' and v3 = 'Z' then v1||chr(ASCII(v2)+1)||'0'
when p1= 0 and p2 = 0 and p3 = 1 and v2 = '9' and v3 = 'Z' then v1||chr(65)||'0'
when p1= 0 and p2 = 1 and p3 = 0 and v3 != '9' then v1||v2||chr(ASCII(v3)+1)
when p1= 0 and p2 = 1 and p3 = 0 and v3 = '9' then v1||v2||chr(65)
when p1= 0 and p2 = 1 and p3 = 1 and v3 != 'Z' then v1||v2||chr(ASCII(v3)+1)
when p1= 0 and p2 = 1 and p3 = 1 and ASCII(v2) between 65 and 89 and v3 = 'Z' then v1||chr(ASCII(v2)+1)||'0'
when p1= 0 and p2 = 1 and p3 = 1 and v1 != '9' and v2 = 'Z' and v3 = 'Z' then chr(ASCII(v1)+1)||'0'||'0'
when p1= 0 and p2 = 1 and p3 = 1 and v1 = '9' and v2 = 'Z' and v3 = 'Z' then chr(65)||'00'
when p1= 1 and p2 = 0 and p3 = 0 and v3 != '9' then v1||v2||chr(ASCII(v3)+1)
when p1= 1 and p2 = 0 and p3 = 0 and v3 = '9' then v1||v2||chr(65)
when p1= 1 and p2 = 0 and p3 = 1 and v3 != 'Z' then v1||v2||chr(ASCII(v3)+1)
when p1= 1 and p2 = 0 and p3 = 1 and v2 != '9' and v3 = 'Z' then v1||chr(ASCII(v2)+1)||'0'
when p1= 1 and p2 = 0 and p3 = 1 and v2 = '9' and v3 = 'Z' then v1||chr(65)||'0'
when p1= 1 and p2 = 1 and p3 = 0 and v3 != '9' then v1||v2||chr(ASCII(v3)+1)
when p1= 1 and p2 = 1 and p3 = 0 and v3 = '9' then v1||v2||chr(65)
when p1= 1 and p2 = 1 and p3 = 1 and v3 != 'Z' then v1||v2||chr(ASCII(v3)+1)
when p1= 1 and p2 = 1 and p3 = 1 and v2 != 'Z' and v3 = 'Z' then v1||chr(ASCII(v2)+1)||'0'
when p1= 1 and p2 = 1 and p3 = 1 and v1 != 'Z' and v2 = 'Z' and v3 = 'Z' then chr(ASCII(v1)+1)||'00'
else '0'
end)
into return_val
from
(
select
NVL(LENGTH(TRIM(TRANSLATE(substr(start_value,1,1), ' +-.0123456789',' '))),0) p1,
NVL(LENGTH(TRIM(TRANSLATE(substr(start_value,2,1), ' +-.0123456789',' '))),0) p2,
NVL(LENGTH(TRIM(TRANSLATE(substr(start_value,3,1), ' +-.0123456789',' '))),0) p3,
NVL(substr(start_value,1,1),0) v1,
NVL(substr(start_value,2,1),0) v2,
NVL(substr(start_value,3,1),0) v3,
start_value
from dual
);
else
return_val := '0';
end if;
return return_val;
end;