?
Solved

Creating alphanumeric sequence generator

Posted on 2004-08-16
11
Medium Priority
?
5,349 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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.
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

765 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