Solved

Creating alphanumeric sequence generator

Posted on 2004-08-16
11
5,135 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
11 Comments
 
LVL 12

Accepted Solution

by:
catchmeifuwant earned 34 total points
Comment Utility
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
Comment Utility
I am afraid you can get it manually by pl/sql only.
0
 

Author Comment

by:sharmapradeepk
Comment Utility
friend

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

Thanks
0
 
LVL 12

Expert Comment

by:catchmeifuwant
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Assisted Solution

by:riazpk
riazpk earned 33 total points
Comment Utility
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
Comment Utility
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
Comment Utility
oh yeah...now i understood what you meant...thanks man
0
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 33 total points
Comment Utility
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
Comment Utility
Agreed......26,000 Values are tooooo small for todays databases. What willl happen after that ?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to recover a database from a user managed backup

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now