How to generate random boolean value in sql server 2008?
You can use
CRYPT_GEN_RANDOM(1) % 2
The advantages over RAND
are that it is stronger cryptographically (you may not care) and that if inserting multiple rows it is re-evaluated for each row.
DECLARE @T TABLE(
B1 BIT,
B2 BIT);
INSERT INTO @T
SELECT TOP 10 CRYPT_GEN_RANDOM(1)%2,
CAST(ROUND(RAND(), 0) AS BIT)
FROM master..spt_values
SELECT *
FROM @T
would give the same value in all rows for the second column
If you are only generating one row, you could use something as simple as:
SELECT CAST(ROUND(RAND(),0) AS BIT)
However, if you are generating more than one row, RAND()
will evaluate to the same value for every row, so please see Martin Smith's answer.