Search code examples
oracle-databaseplsqloracle12c

Generate Random String in PL/SQL (oracle 12c)


I'm trying to generate a Random String using PL/SQL with only 2 fixed words. It's this possible?


Solution

  • Is this what you're looking for?

    SQL> with
      2  -- two fixed words
      3  test as
      4    (select 'fixed words' col from dual),
      5  -- split them to rows
      6  inter as
      7    (select level lvl, regexp_substr(col, '.', 1, level) let
      8     from test
      9     connect by level <= length(col)
     10    )
     11  -- aggregate them back, randomly
     12  select listagg(let, '') within group (order by dbms_random.value(1, max_lvl)) result
     13  from inter
     14  join (select max(lvl) max_lvl from inter) on 1 = 1;
    
    RESULT
    --------------------------------------------------------------------------------
    reiosdwxf d
    
    SQL> /
    
    RESULT
    --------------------------------------------------------------------------------
    fe ixoddrws
    
    SQL> /
    
    RESULT
    --------------------------------------------------------------------------------
     wdxeorsdfi
    
    SQL>