[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating alphanumeric sequence generator

Posted on 2004-08-16
11
Medium Priority
?
5,486 Views
Last Modified: 2008-01-09
How to create sequence generator for generating following sequence:
A0001, A0002,....B0001,B0002,.....,Z9999
0
Comment
Question by:sharmapradeepk
9 Comments
 
LVL 12

Accepted Solution

by:
catchmeifuwant earned 136 total points
ID: 11807500
Use this PL/SQL script for generating the sequence..

============================
declare
v_data varchar2(5);
v_data_1 varchar2(5);
begin
for rec_alpha in 65..90
loop
for rec_num in 0..9999
loop
v_data := chr(rec_alpha);
select decode(length(rec_num),1,'000'||to_char(rec_num),2,'00'||to_char(rec_num),3,'0'||to_char(rec_num),to_char(rec_num))
into v_data_1
from dual;
v_data := v_data||v_data_1;
dbms_output.put_line(v_data);
end loop;
end loop;
end;
/

============================
0
 

Expert Comment

by:884813
ID: 11807504
I am afraid you can get it manually by pl/sql only.
0
 

Author Comment

by:sharmapradeepk
ID: 11807584
friend

i want to use only
"create sequence ...."
for generating sequence, not procedure, function or other stuff

Thanks
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11807599
OK...in any case Sequence cannot generate an alphanumeric sequence.

For what purpose are you looking for?

You can import same logic into a trigger etc if your purpose is to generate an ID or something...


0
 
LVL 13

Assisted Solution

by:riazpk
riazpk earned 132 total points
ID: 11808032
Its too easy...here is a test case (@@ catchmeifuwant i beg to differ here ):


SQL> create sequence my_seq;

Sequence created.

Elapsed: 00:00:00.16
SQL> ed
Wrote file afiedt.buf

  1  select my_seq.nextval,
  2                 chr( ascii('A')+mod(trunc(my_seq.nextval/100000),  26) ) ||
  3                 to_char(mod(my_seq.nextval,100000), 'fm00000')
  4*       from dual
SQL> /

   NEXTVAL                                                                                                                                            
==========                                                                                                                                            
CHR(ASCII('A')+MOD(TRUNC(MY_SE                                                                                                                        
======================================================================================================================================================
         1                                                                                                                                            
A00001                                                                                                                                                
                                                                                                                                                     

Elapsed: 00:00:00.16
SQL> ed
Wrote file afiedt.buf

  1  select my_seq.nextval,
  2                 chr( ascii('A')+mod(trunc(my_seq.nextval/100000),  26) ) ||
  3                 to_char(mod(my_seq.nextval,100000), 'fm00000')
  4*       from dual
SQL> /

   NEXTVAL                                                                                                                                            
==========                                                                                                                                            
CHR(ASCII('A')+MOD(TRUNC(MY_SE                                                                                                                        
======================================================================================================================================================
         2                                                                                                                                            
A00002                                                                                                                                                
                                                                                                                                                     

Elapsed: 00:00:00.28
SQL> /

   NEXTVAL                                                                                                                                            
==========                                                                                                                                            
CHR(ASCII('A')+MOD(TRUNC(MY_SE                                                                                                                        
======================================================================================================================================================
         3                                                                                                                                            
A00003                                                                                                                                                
                                                                                                                                                     

Elapsed: 00:00:00.28
SQL> /

   NEXTVAL                                                                                                                                            
==========                                                                                                                                            
CHR(ASCII('A')+MOD(TRUNC(MY_SE                                                                                                                        
======================================================================================================================================================
         4                                                                                                                                            
A00004                                                                                                                                                
                                                                                                                                                     

Elapsed: 00:00:00.44
SQL> spo off
0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11808106
No problem Riazpk...

well at the end of the day, there's something applied onto the sequence to get the desired results(different logic ofcourse).

What I was saying was "A sequence cannot generate AlphaNumeric series directly".

Look at his comment :
--------------
friend

i want to use only
"create sequence ...."
for generating sequence, not procedure, function or other stuff

--------

which is NOT possible.

Anyway thanks for the piece of code...pretty nice job there :-) Cheers !
0
 
LVL 13

Expert Comment

by:riazpk
ID: 11812029
oh yeah...now i understood what you meant...thanks man
0
 
LVL 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 132 total points
ID: 11814496
Do you need to generate numbers A0001 - A9999, then B0001 - B9999, etc. through Z9999?  What should happen after you generate 26,000 values?

I agree with the others, you cannot do this simply with a sequence.  What you can do is create a sequence that will generate values from 1 - 9999, then start over with 1 again, like this:
create sequence my_seq maxvalue 9999 cycle;
But you will have to write a mechanism to keep track of which letter value to concatenate with the value from the sequence.  You may be able to use a table that you create named: "alphabet" and populate with 26 records: A - Z.  You may need a flag and date column in this table also, to keep track of which alpha value to use, and when it is time to get the next one.  A PL\SQL function can do this for you.
0
 
LVL 13

Expert Comment

by:riazpk
ID: 11818441
Agreed......26,000 Values are tooooo small for todays databases. What willl happen after that ?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question