venky80
venky80

Reputation: 955

How to update a table column with random values within a range in Oracle?

I have a table with 3 columns: - PRIMARY_KEY - AMOUNTS - RAND_AMOUNTS (all nulls)

I want the RAND_AMOUNTS column to be populated with following formula:

AMOUNT*(0-100 random value)/100

So for example if the amount row let us suppose is 10 and the random value generated for the row is 10 then RAND_AMOUNT should be 10 * 10 / 100 = $1

Upvotes: 3

Views: 20572

Answers (3)

HAL 9000
HAL 9000

Reputation: 3985

update yourtable set rand_amounts = dbms_random.value * amount;

Upvotes: 4

cagcowboy
cagcowboy

Reputation: 30838

Using:

dbms_random.value(lower-value-limit, upper-value-limit)

should do what you're looking for.

Upvotes: 2

user330315
user330315

Reputation:

dbms_random() is what you are looking for:

UPDATE the_table
   SET rand_amount = (amount * dbms_random.value(0, 100) ) / 100
WHERE amount IS NOT NULL

Upvotes: 8

Related Questions