Home > Ask the Oracle Experts > (Archive) SQL and PL/SQL Questions & Answers > Generating a 10 character alphanumeric sequence
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Generating a 10 character alphanumeric sequence

Azim Fahmi EXPERT RESPONSE FROM: Azim Fahmi

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 29 April 2005
I need to create a 10 character alphanumeric sequence for a client. The sequence is to be static like this: a111a11a11a. If it's a number, increment 1 until you reach 9, then move to the next column, if it's a letter, increment 1 until z, then move to the next column. Can you suggest a simple solution for this?

>
EXPERT RESPONSE

Try the following function that utilizes the built-in functions ASCII and CHR. I have created the function for an arbitrary length of sequence. However, the function will not attempt to exceed the maximum length of the string. It the original sequence is 'a111a11a11a'(in your example the length is 10) the maximum the function will return is 'z999z99z99z'. Also, I have also tested the boundary conditions of a single character sequence of 9 and z.

So, with much further ado, here is the function:

create or replace function return_next_seq (curr_sequence IN OUT VARCHAR2) 
return varchar2 is

 retval VARCHAR2(4000) := null;

 eval_digit CHAR(1) := null;

 original_sequence VARCHAR2(4000) := curr_sequence;

begin

  for j in REVERSE 1..length(curr_sequence) loop  -- Using reverse to know 
        -- the exact digit position

     eval_digit := substr(curr_sequence, length(curr_sequence));

     IF (ASCII(eval_digit) between 49 and 56) OR 
        (ASCII(eval_digit) between 97 and 121) THEN     
       eval_digit := CHR(ASCII(eval_digit) +1);
       curr_sequence := substr(curr_sequence,1,length(curr_sequence)-1);
       retval := curr_sequence || eval_digit || substr(original_sequence, 
length(curr_sequence || eval_digit)+1);
       exit;

     ELSE  -- move to the next digit leaving the evaluated digit untouched.

     curr_sequence := substr(curr_sequence,1,length(curr_sequence)-1);
 
     END IF;   
  end loop;
  IF retval is null THEN
    return 'MAXIMUM value of sequence reached';
  END IF;
  return retval;
end;
/


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
(Archive) SQL and PL/SQL
ORA-01654 even though maxextents defined as unlimited
Read, process and write a text file to the database
Concatenating a null character affecting performance
Working with InterDev and DreamWeaver
Line placement in an EMESSAGE
Searching for stored reservation dates in a database
Specifying a column as NULL
Write a SQL script with optional arguments
Error message while trying to write a Pl/SQL block
Why do many DBMS push their own programming languages?

Oracle PL/SQL
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
ORA-01422 error when procedure returns more than one row
Calling procedure inside another procedure in anonymous block
How to import comma-delimited text file to Oracle table
Oracle updates Microsoft developer tools
PLS-00103 errors
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts