Generate Upper and Lowercase Alphanumeric Random String in Oracle

前端 未结 6 642
北荒
北荒 2021-02-05 14:55

How does one generate an upper and lowercase alphanumeric random string from oracle?

I have used select DBMS_RANDOM.STRING(\'x\', 10) from dual to generate

6条回答
  •  失恋的感觉
    2021-02-05 15:27

    You could start with the Printable option, then strip out any non-alphanumerics:

    select SUBSTR(
             TRANSLATE(dbms_random.string('p',100)
                ,'A`~!@#$%^&*()-=_+[]\{}|;'':",./<>?'
                ,'A')
           ,1,10) from dual;
    

    (Note: very rarely, this will return less than 10 characters)

    or, map the offending characters to other letters and numbers (although this would reduce the randomness quite a bit):

    select TRANSLATE(dbms_random.string('p',10)
                ,'A`~!@#$%^&*()-=_+[]\{}|;'':",./<>? '
                ,'A' || dbms_random.string('x',33)) from dual;
    

    Great question, by the way.

    Now, for my bonus points:

    The reason Oracle didn't implement this is because no-one asked for it, and it probably is not a high priority.

提交回复
热议问题