Saturday, 7 March 2015

Generating Random Values in Oracle


It can be done by using the package DBMS_RANDOM.
Different functions available in DBMS_RANDOM package :
  • RANDOM
  • VALUE
  • STRING

Examples:
  1.  select SYS.DBMS_RANDOM.RANDOM FROM DUAL;
    It will return any random value
  2. SELECT SYS.DBMS_RANDOM.VALUE FROM DUAL;
    It will return a numeric value between 0 and 1
  3. SELECT SYS.DBMS_RANDOM.VALUE(1,1000) FROM DUAL;
    Returns any value between 1 and 1000.
    We need to use TRUNC function to remove the decimal is generated by this package
    SELECT TRUNC(SYS.DBMS_RANDOM.VALUE(1,1000)) FROM DUAL;
  4. select SYS.DBMS_RANDOM.STRING('U',8) from DUAL;
    Returns the string with the 8 characters in uppercase
  5. select SYS.DBMS_RANDOM.STRING('L',8) from DUAL;
    Returns the string with the 8 characters in lowercase
    A --> Alpha numeric
    X --> Alphanumeric with lower case



No comments:

Post a Comment