Solved

Creating alphanumeric sequence generator

Posted on 2004-08-16
11
5,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 12

Accepted Solution

by:
catchmeifuwant earned 34 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 33 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 33 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create Index on a Materialized View 5 59
Oracle mutateing errors 3 23
Updating a temp table inside a PL/SQL block 3 63
replicate in oracle 13 42
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

738 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