DBMS Random Reference

What?
How to use the randomization features of Oracle PL/SQL.

Why?
I've been tasked with writing a stored procedure that scrambles data for developers working with some of our databases containing sensitive data.

How?
copyraw
SELECT DBMS_RANDOM.option1[(option2)] FROM DUAL;
  1.  SELECT DBMS_RANDOM.option1[(option2)] FROM DUAL; 
option1 can be:
  • RANDOM
  • VALUE
  • STRING
option2 can be:
  • U (for Uppercase)
  • L (for Lowercase)
  • A (for AlphaNumeric)
  • X (for Alphanumeric with upper case alphabets)
  • P (for Printable characters only)

Usage
copyraw
select dbms_random.random from dual;  
-- yields random number, eg. 68489408

select dbms_random.value from dual;  
-- yields values between 0 and 1

select dbms_random.value(1,1000) myAlias from dual; 
-- yields number between 1 and 1000

select dbms_random.string('U', 10) myAlias from dual;  
-- yields uppercase string of 10 letters

select dbms_random.string('L', 10) myAlias from dual;  
-- yields lowercase string of 10 letters

select dbms_random.string('A', 10) myAlias from dual;  
-- yields string of 10 mixed upper and lowercase letters 

select dbms_random.string('X', 10) myAlias from dual;  
-- yields string of 10 mixed uppercase letters and digits

select dbms_random.string('P', 10) myAlias from dual;  
-- yields string of 10 printable characters (includes symbols/punctuation)
  1.  select dbms_random.random from dual; 
  2.  -- yields random number, eg. 68489408 
  3.   
  4.  select dbms_random.value from dual; 
  5.  -- yields values between 0 and 1 
  6.   
  7.  select dbms_random.value(1,1000) myAlias from dual; 
  8.  -- yields number between 1 and 1000 
  9.   
  10.  select dbms_random.string('U', 10) myAlias from dual; 
  11.  -- yields uppercase string of 10 letters 
  12.   
  13.  select dbms_random.string('L', 10) myAlias from dual; 
  14.  -- yields lowercase string of 10 letters 
  15.   
  16.  select dbms_random.string('A', 10) myAlias from dual; 
  17.  -- yields string of 10 mixed upper and lowercase letters 
  18.   
  19.  select dbms_random.string('X', 10) myAlias from dual; 
  20.  -- yields string of 10 mixed uppercase letters and digits 
  21.   
  22.  select dbms_random.string('P', 10) myAlias from dual; 
  23.  -- yields string of 10 printable characters (includes symbols/punctuation) 


Source: derived from: http://www.databasejournal.com/features/oracle/article.php/3341051/Generating-random-numbers-and-strings-in-Oracle.htm
Category: Oracle PL/SQL :: Article: 431

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.