Skip to content
Advertisement

Is there a shorter way to update a column with range values on Oracle SQL?

The update script will be like

UPDATE TABLE_A
SET COLUMN_A = (range values (1,10)
WHERE  COLUMN_B = 'Descriptions'

So that when I run the query below

SELECT COLUMN_B, min(COLUMN_A ), max(COLUMN_A )
FROM TABLE_A
WHERE COLUMN_B = 'Descriptions'
group by COLUMN_B ;

COLUMN_A has minimum value of 1 and maximum value of 10.

Advertisement

Answer

UPDATE TABLE_A 
   SET COLUMN_A = CEIL(DBMS_RANDOM.VALUE * 10) 
 WHERE COLUMN_B = 'Descriptions';

There is no “ranges” function in oracle, but you can emulate that using dbms_random which generates a value between 0 an 1. Multiply it by 10 so you get a value between 0 and 10 (10 will not be included). Round up (using CEIL function) so you get values 1 thru 10. Note that you will get random values between 1 and 10 in this example, there is no guarantee you have values 1 and 10 included and there will likely be duplicates.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement