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: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience 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 bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.