Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Alphabetical Sequence

Eric.SacramentoApr 5 2018 — edited Apr 6 2018

Hi guys,

I am trying to do an alphabetical sequence using sql query with 5 characters.

I am stuck on this query. Can you help me to get a sequence between AAAAA to ZZZZZ ?

Thank you

select * from(

select i1,i2,i3,i4,i5,l,

               case when i5 > 0 then chr(i5+ascii('A')-1) end ||

               case when i4 > 0 then chr(i4+ascii('A')-1) end ||

               case when i3 > 0 then chr(i3+ascii('A')-1) end ||

               case when i2 > 0 then chr(i2+ascii('A')-1) end ||

               chr(i1+ascii('A')-1)

               str

      from (

    select mod((level-1),27) i1,

           mod( trunc((level-0.1)/27),27) i2,

           mod( trunc((level-0.1)/27/27),27) i3,

           mod( trunc((level-0.1)/27/27/27),27) i4,

           mod( trunc((level-0.1)/27/27/27),27) i5, -- even if 4x /27 is not working

           level l

     from dual

   connect by level <= 40000

  

          )

    where i1 <> 0

      and NOT( l>= 27*27 and i2 = 0)

      and NOT( l>= 27*27*27 and i3 = 0)

      and NOT( l>= 27*27*27*27 and i4 = 0)

      and NOT( l>= 27*27*27*27*27 and i5 = 0)

    order by i1,i2,i3,i4,i5)

where

--length(str) = 5 and

str = 'AAZZZ'

order by i5,i4,i3,i2,i1,l;

 

This post has been answered by Paulzip on Apr 5 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 4 2018
Added on Apr 5 2018
20 comments
1,546 views