Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Random String from List

Tags:

sql

random

oracle

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.

like image 419
Patrick Gregorio Avatar asked Mar 07 '16 20:03

Patrick Gregorio


People also ask

How do I randomize a string in SQL?

If you need a string of random digits up to 32 characters for test data or just need some junk text to fill a field, SQL Server's NEWID() function makes this simple. NEWID() is used to create a new GUID (globally unique identifier), and we can use that as a base to get a string of random characters.

Is there a random function in SQL?

RAND() function : This function in SQL Server is used to return a random decimal value and this value lies in the range greater than and equal to zero (>=0) and less than 1. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression “FLOOR(i + RAND() * (j − i))”.

How do you generate random unique strings in node JS?

Example 1: Generate Random Strings In the above example, the Math. random() method is used to generate random characters from the specified characters (A-Z, a-z, 0-9). The for loop is used to loop through the number passed into the generateString() function. During each iteration, a random character is generated.


1 Answers

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
like image 54
Hart CO Avatar answered Oct 01 '22 13:10

Hart CO