When I combine random integer generation with CHOOSE() , I get unexpected NULL values.
The following should only return the letters a - e ; instead, NULL also included in the values:
Query:
;WITH cte AS (SELECT 1 RN UNION ALL SELECT RN + 1 FROM cte WHERE RN < 100) SELECT DISTINCT CHOOSE(1 + ABS(CRYPT_GEN_RANDOM(8)%5),'a','b','c','d','e','f') FROM cte
Results:
NULL a b c d e
Expected results:
a b c d e
Random number generation works as expected, returning only 1-5 values:
;WITH cte AS (SELECT 1 RN UNION ALL SELECT RN + 1 FROM cte WHERE RN < 50) SELECT 1 + ABS(CRYPT_GEN_RANDOM(8)%5) FROM cte
Demo: SQL Fiddle
CHOOSE() works as follows (index starts at 1):
SELECT CHOOSE(3,'dog','cat','horse','fish') -- horse SELECT CHOOSE(8,'dog','cat','horse','fish') -- NULL
Using random number generation in functions works fine for LEFT() , RIGHT() , CHAR() , etc. The workaround will be fine, but basically I'm curious why I get NULL values ββat all.
sql sql-server-2012
Hart CO
source share