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.
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.
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))”.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With