从列表中返回随机值

时间:2015-12-21 14:38:44

标签: sql-server sql-server-2014

我正在使用SQL Server 2014,并希望利用新功能CHOOSE和RAND。基本上想从列表中返回随机颜色。

类似的东西:

Select CHOOSE(RAND(29), 'bg-blue', 'bg-blue-madison', 'bg-blue-hoki', 'bg-blue-steel', 'bg-blue-chambray',
                    'bg-green-meadow', 'bg-green', 'bg-green-seagreen', 'bg-green-turquoise', 'bg-green-haze', 'bg-green-jungle',
                    'bg-red', 'bg-red-pink', 'bg-red-sunglo', 'bg-red-intense', 'bg-red-thunderbird', 'bg-red-flamingo',
                    'bg-yellow', 'bg-yellow-gold', 'bg-yellow-casablanca', 'bg-yellow-lemon',
                    'bg-purple', 'bg-purple-plum', 'bg-purple-studio', 'bg-purple-seance',
                    'bg-grey-cascade', 'bg-grey-silver', 'bg-grey-steel', 'bg-grey-gallery') AS Colour

有可能吗?

4 个答案:

答案 0 :(得分:6)

您必须在下面使用RAND + ROUND才能获得从1到29的整数:

DECLARE @num INT = ROUND(RAND()*28,0) + 1

SELECT CHOOSE(@num, 'bg-blue', 'bg-blue-madison', 'bg-blue-hoki', 'bg-blue-steel', 'bg-blue-chambray',
                    'bg-green-meadow', 'bg-green', 'bg-green-seagreen', 'bg-green-turquoise', 'bg-green-haze', 'bg-green-jungle',
                    'bg-red', 'bg-red-pink', 'bg-red-sunglo', 'bg-red-intense', 'bg-red-thunderbird', 'bg-red-flamingo',
                    'bg-yellow', 'bg-yellow-gold', 'bg-yellow-casablanca', 'bg-yellow-lemon',
                    'bg-purple', 'bg-purple-plum', 'bg-purple-studio', 'bg-purple-seance',
                    'bg-grey-cascade', 'bg-grey-silver', 'bg-grey-steel', 'bg-grey-gallery') AS Test

为了更准确,您可以使用CEILING作为@GarethD评论如下:

DECLARE @num INT = CEILING(RAND()*29)

正在使用 SQL-FIDDLE

答案 1 :(得分:6)

您没有提到您知道这一点,如果您不知道这一点,我会再给您一个解决方案:

SELECT TOP 1 v FROM(VALUES('bg-blue'), ('bg-blue-madison'), ('bg-blue-hoki'))t(v)
ORDER BY NEWID()

答案 2 :(得分:4)

试试这个

Declare @RandVal INT
SELECT @RandVal = ABS(Checksum(NewID()) % 29) + 1
SELECT @RandVal

Select CHOOSE(@RandVal, 'bg-blue', 'bg-blue-madison', 'bg-blue-hoki', 'bg-blue-steel', 'bg-blue-chambray',
                    'bg-green-meadow', 'bg-green', 'bg-green-seagreen', 'bg-green-turquoise', 'bg-green-haze', 'bg-green-jungle',
                    'bg-red', 'bg-red-pink', 'bg-red-sunglo', 'bg-red-intense', 'bg-red-thunderbird', 'bg-red-flamingo',
                    'bg-yellow', 'bg-yellow-gold', 'bg-yellow-casablanca', 'bg-yellow-lemon',
                    'bg-purple', 'bg-purple-plum', 'bg-purple-studio', 'bg-purple-seance',
                    'bg-grey-cascade', 'bg-grey-silver', 'bg-grey-steel', 'bg-grey-gallery') AS Colour

答案 3 :(得分:4)

RAND函数将种子值作为参数,而不是最大随机值。您需要将随机数的结果乘以您需要的最大值,以获得该范围内的随机数。

当我测试这个时,我必须先将随机值传递给变量,否则它有时会返回null。正如Gareth D在评论中所提到的,这是因为每次检查选项是否相等时,函数评估RAND()的方式都会被调用一次。

DECLARE @counter smallint;

SET @counter = (RAND()*28)+1;

Select @counter, CHOOSE(@counter, 'bg-blue', 'bg-blue-madison', 'bg-blue-hoki', 'bg-blue-steel', 'bg-blue-chambray',
                    'bg-green-meadow', 'bg-green', 'bg-green-seagreen', 'bg-green-turquoise', 'bg-green-haze', 'bg-green-jungle',
                    'bg-red', 'bg-red-pink', 'bg-red-sunglo', 'bg-red-intense', 'bg-red-thunderbird', 'bg-red-flamingo',
                    'bg-yellow', 'bg-yellow-gold', 'bg-yellow-casablanca', 'bg-yellow-lemon',
                    'bg-purple', 'bg-purple-plum', 'bg-purple-studio', 'bg-purple-seance',
                    'bg-grey-cascade', 'bg-grey-silver', 'bg-grey-steel', 'bg-grey-gallery') AS Colour