Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate only o or 1 for a bit column?

Tags:

t-sql

I need to fill a bit column only with 0 or 1, I tried the following, which only gave me 1:

convert(bit, 2*rand())

How can I make it work?

like image 820
Lisa Avatar asked Nov 26 '25 06:11

Lisa


1 Answers

If you need multiple rows, and you don't care about the specific distribution of values, something like the following may be appropriate:

CONVERT(bit,SUBSTRING(CONVERT(binary(18),newid()),1,1)&1)

This will be evaluated once per row

like image 115
Damien_The_Unbeliever Avatar answered Nov 28 '25 15:11

Damien_The_Unbeliever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!