Skip to Main Content
  • Questions
  • To arrive alphanumeric value from a string

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Harsha.

Asked: December 12, 2018 - 5:19 pm UTC

Last updated: December 17, 2018 - 2:03 am UTC

Version: 11.0

Viewed 1000+ times

You Asked

Hi Tom,

I have a problem to arrive unique alphanumeric value

I have to arrive unique short name(5digit) from names and insert into the table.

We have to check existing short names and arrive next heirarchy.


Eg.The names are like below

Harsha vardhan
Harsha sree
Harsha kumar
Harsha kanth

I need the following result

HARSH
HARSA
HARSB
.
.
.
HARSZ
HARA0
HARA1
.
.
HARA9
HARB0
.
.
HARB9
.
.
HARZ9
HAA01
..


PLS HELP ME OUT


and Connor said...

A test case would not have gone astray.....sigh

But this should get you going

SQL> select
  2    rownum,
  3    trunc((rownum-1)/36),
  4    mod(rownum-1,36)
  5  from dual
  6  connect by level <= 60;

SQL>
SQL>
SQL> with chars as ( select 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' x from dual )
  2  select
  3    rownum,
  4    substr(x,trunc((rownum-1)/36),1),
  5    substr(x,mod(rownum-1,36),1)
  6  from chars
  7  connect by level <= 60;



Warning: This also means you can only single stream data into the table (ie, you cannot have more than session doing this). I'd advise you to perhaps rethink this.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

SQL

Harsha, December 13, 2018 - 3:45 pm UTC

I am very sorry i am not able to explain the issue neatly.

I am having the table with fields as name,short name . I had given an option in front end to enter name of the employee. I want to arrive unique short name ( max of 5digits) and should check whether the same short name exists already in table and arrive next possible short name. I am expecting around 5 lacks short names to arrive.

First check 5 digit, next 4 digit,3 digit to arrive unique code....

Can be SQL or Function to arrive the result




Pls help....
Connor McDonald
December 17, 2018 - 2:03 am UTC

So you can see how to generate the suffixes per my previous answer. So the list of potential names you'd like to use is something like:

with 
chars as ( select 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' x from dual ),
suffixes as (
 select
   rownum r,
   substr(x,trunc((rownum-1)/36),1) c1,
   substr(x,mod(rownum-1,36),1) c2
 from chars
 connect by level <= 36*36
),
potentials as
( select substr(:input,1,5)  x from dual
  union all
  select substr(:input,1,4)||c2 from suffixes where rownum <= 36
  union all
  select substr(:input,1,3)||c1||c2 from suffixes where rownum <= 36*36
)
select * 
from potentials
where not exists ( select 1 from emp_table where short_name = potentials.x )
and rownum = 1


with my same caveats about concurrency and the like.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library