Good afternoon! Looking for some advice specific to Postgres. It does not have an equivalent to CHOOSE(), but I would like to use it to pick a random string from a list. I.e.:
SELECT CHOOSE ('red', 'yellow', 'blue', 'green');
Is there an efficient way of replicating this in Postgres? Thanks!
Attempted to use CHOOSE() but it is not present in Postgres nor a known equivalent. Considering using RANDOM() to generate a value and making CASE statements to pick a string based on the value.
2
Answers
Create your own function:
However, this function is limited to a maximum of 100 arguments. If you need more, you have to use an array as input:
You can use
array_sample
with a parameter of1
, then take the first value.db<>fiddle