How to create alpha numeric sequence.
First, the character "A" can be represented as CHR(65), "B" as CHR(66),and so on.
The answer lies in using two Oracle functions: MOD and TRUNC.
-- MOD gives the remainder of two numbers. For example, MOD(50,26) gives 24, which is the remainder of 50/26.
-- TRUNC gives just the integer value of a number. So TRUNC(50/26) gives 1, which is the integer value of 1.92307692...
So, you want a sequence of characters AAA, AAB, AAC, ...
The last character is simply chr(MOD(x,26)+64)
The second-to-last character is chr(MOD(TRUNC(x/26),26)+65)
The third-to-last character is chr(MOD(TRUNC(x/26)/26),26)+65)
Putting it all together:
SELECT chr(MOD(TRUNC(x/26)/26),26)+65)|| chr(MOD(TRUNC(x/26),26)+65)||
chr(MOD(x,26)+64)
You can replace "x" above with SEQUENCE_NAME.NEXTVAL. This is good for a three- character sequence.
The answer lies in using two Oracle functions: MOD and TRUNC.
-- MOD gives the remainder of two numbers. For example, MOD(50,26) gives 24, which is the remainder of 50/26.
-- TRUNC gives just the integer value of a number. So TRUNC(50/26) gives 1, which is the integer value of 1.92307692...
So, you want a sequence of characters AAA, AAB, AAC, ...
The last character is simply chr(MOD(x,26)+64)
The second-to-last character is chr(MOD(TRUNC(x/26),26)+65)
The third-to-last character is chr(MOD(TRUNC(x/26)/26),26)+65)
Putting it all together:
SELECT chr(MOD(TRUNC(x/26)/26),26)+65)|| chr(MOD(TRUNC(x/26),26)+65)||
chr(MOD(x,26)+64)
You can replace "x" above with SEQUENCE_NAME.NEXTVAL. This is good for a three- character sequence.
Comments
Post a Comment