出于测试目的,我想通过将colums中的位设置为随机值来更新表。
update [Planned]
set [IsPlannable] = 1 * rand(cast(cast(newid() as binary(8)) as int))
WHERE [ComputerID] > 100
它似乎确实应该,但不是我想要的方式。我想问题是结果大部分时间都会高于1。
我如何将随机位翻转为随机值?
答案 0 :(得分:6)
1 *
仍然会产生一个小数&假设cast(0.1 as bit)
将产生1,cast(0.9 as bit)
将更新全部设置为1。
你可以;
update Planned set IsPlannable = case when rand(cast(newid() as binary(8))) < 0.5 then 0 else 1 end
答案 1 :(得分:1)
根据您必须使用的位字段数,您可以使用以下内容生成所有可能的设置:
with test as (
select 0 as myId, cast(0 as bit) col1, cast(0 as bit) col2, cast(0 as bit) col3
union all
select myId + 1,
case when myId & 1 = 1 then cast(1 as bit) else cast(0 as bit) end,
case when myId & 2 = 2 then cast(1 as bit) else cast(0 as bit) end,
case when myId & 4 = 4 then cast(1 as bit) else cast(0 as bit) end
from test
where myId<100
)
select distinct col1, col2, col3 from test
答案 2 :(得分:0)
怎么样
cast(round(rand(), 0) as bit)