Solved

Creating alphanumeric sequence generator

Posted on 2004-08-16
11
5,173 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
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
 
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
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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot open form error 6 65
report returning null 21 79
Oracle and DateTime math 6 25
Question About Creating Primary Key Constraints on Oracle Tables... 5 38
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

932 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

14 Experts available now in Live!

Get 1:1 Help Now