Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate random boolean value in sql server 2008?

Im writing a database for some kind of university and there is a table named

Contact_Assign Its parameters are:

Is_Instructor       UD_BOOLEAN NOT NULL,
Is_TeacherAssistant UD_BOOLEAN NOT NULL,
Is_Student          UD_BOOLEAN NOT NULL,
Registration_ID     UD_ID      NOT NULL,
Contact_ID          UD_ID      NOT NULL,

now I want to insert dummy data in this table but I have no idea how can I do this for the boolean parameters.

PS. UD_BOOLEAN is

CREATE TYPE UD_BOOLEAN FROM BIT

any idea how?

like image 364
E mad Avatar asked Dec 15 '13 17:12

E mad


1 Answers

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

like image 193
Martin Smith Avatar answered Oct 04 '22 00:10

Martin Smith