7

I want to choose from a list of strings and assign that as the value of one of the columns for my SELECT.

Something like the following:

SELECT id, name, GET_RANDOM_TYPE('Basic', 'Silver', 'Gold', 'Premium') AS type
FROM tbl

I'm just doing some tests hence why I need this.

2 Answers 2

20

Not terribly familiar with oracle, but perhaps you can simply use round(dbms_random.value(1,4)) in conjunction with a CASE expression:

SELECT id,
       CASE round(dbms_random.value(1,4)) 
            WHEN 1 THEN 'Basic' 
            WHEN 2 THEN 'Silver' 
            WHEN 3 THEN 'Gold' 
            WHEN 4 THEN 'Premium' 
       END AS type
FROM table
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this works. It's the same as what Ravindra HV answered. You just happen to provide the query.
nevermind what I said about ROUND. It still needs it to work. I was using > or < so it worked on my query. Since I random from 1-10.
0

Create a table with your list of values that has a number as a primary key.

Then

Select  your_text
from your_random_table
where ID = TRUNC(DBMS_RANDOM.value(1,10));

The statement above will give you any one 10 pseudo random numbers and assumes you have 10 random values in your table. It's not really random but works for testing. See here.

1 Comment

Is there a way to avoid having to create a new table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.