Tuesday, October 28, 2008

How to implement Alpha Numeric Sequence (Increment AAAAZ to AAAB0)?

I faced the issue of generating the sequence of alphanumeric characters in pl/sql.PL/SQL supports the number sequences but not alpha numeric sequences.So decided to write one algorithm to perform the sequence generation


CREATE OR REPLACE FUNCTION batchseq_next(batchseq VARCHAR2) RETURN VARCHAR2 IS
str varchar2(5) :=batchseq;
inc char(1);
i number(2) :=5;
BEGIN
IF(SUBSTR(str, i, 1) = '9') THEN
str := SUBSTR(str, 1, i -1) || 'A';
ELSIF(SUBSTR(str, i, 1) != 'Z') THEN
str := SUBSTR(str, 1, i -1) || CHR((ASCII(SUBSTR(str, i, 1)) + 1));
else
if(SUBSTR(str, instr(str,'Z',1,1)-1, 1) ='9') then
inc := 'A';
else
inc := CHR((ASCII(SUBSTR(str, instr(str,'Z',1,1)-1, 1)) + 1));
end if;
str := SUBSTR(str, 1, instr(str,'Z',1,1)-2) ||inc|| translate (SUBSTR(str, instr(str,'Z',1,1), i),'Z','0');
END IF;
RETURN str;
END batchseq_next;

Enter your Comments