| Author |
Topic |
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2008-02-11 : 19:14:51
|
| Hi -- Can someone tell me why this SELECT statement sometimes returns NULL? I'm trying to get this to always pick 0 or 1 randomly but NULL values are sometimes being returned (along with some M's and F's at other times). Any ideas? SELECT CASE CAST(ROUND(RAND(), 0) AS BIT) WHEN 0 THEN 'M' --Male WHEN 1 THEN 'F' --Female END Thanks in advance,Bill |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-11 : 19:33:11
|
| no sure why but try this:select when CAST(ROUND(RAND(), 0) AS BIT) = 0 then 'M' else 'F' endBe One with the OptimizerTG |
 |
|
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2008-02-11 : 19:39:47
|
quote: Originally posted by TG no sure why but try this:select when CAST(ROUND(RAND(), 0) AS BIT) = 0 then 'M' else 'F' endBe One with the OptimizerTG
Yeah, I know I could do that but I'm baffled as to why it's happening. If I use your solution then it's not really random since the NULL value comes up about 1/3 of the time. Therefore, 'F' would be much more likely to be returned. What's also interesting is that if I cache the value of "CAST(ROUND(RAND(), 0) AS BIT)" in a bit variable, the NULLs never occur. But in my situation, I'd rather not have to cache the value.Any ideas as to why?Bill |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-02-11 : 19:48:22
|
The rand is never actually returning null. for some reason that style of CASE is causing the problem. I tried this solution for 3000 iterations and came up with a pretty even distribution of 0 and 1:----------- ----------- 0 15281 1469EDIT:Here is the code it tried:create Table #i (i int null)declare @i intset @i = 1set nocount onwhile @i < 1000begin insert #i (i) select CAST(ROUND(RAND(), 0) AS BIT) set @i = @i+1endselect i, count(*) from #i group by idrop table #i Be One with the OptimizerTG |
 |
|
|
billsox
Yak Posting Veteran
74 Posts |
Posted - 2008-02-11 : 19:54:18
|
quote: Originally posted by TG The rand is never actually returning null. for some reason that style of CASE is causing the problem. I tried this solution for 3000 iterations and came up with a pretty even distribution of 0 and 1:----------- ----------- 0 15281 1469Be One with the OptimizerTG
You're right, TG. Thanks. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-12 : 01:47:57
|
| orSELECT CASE number WHEN 0 THEN 'M' --Male WHEN 1 THEN 'F' --Female END from(select top 1 number%2 as number from master..spt_values where type='p' order by newid()) as tMadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-12 : 03:07:04
|
SELECT CAST ABS(CHECKSUM(NEWID())) % 2 WHEN 0 THEN 'Male' ELSE 'Female'END AS Gender E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-12 : 04:26:15
|
quote: Originally posted by Peso SELECT CAST CASE ABS(CHECKSUM(NEWID())) % 2 WHEN 0 THEN 'Male' ELSE 'Female'END AS Gender E 12°55'05.25"N 56°04'39.16"
MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-12 : 04:28:18
|
Need more coffee now!  E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|