Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert random values


Insert random values

Author
Message
n_a_h_k
n_a_h_k
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 10
Dear Friends,

I am wondering if you could give me a hand in this situation.

I am a new user for sql and i am currently work in task that requires me to update random values to my table. I have 1000 rows consist of 0 and 1 values and i would like to randomly change 100 cells that contain 0 values to 1. I would like to know which sql statement can help me to perform this.

Thank you for your assistant in advance.

Regards,
:-)
Dave Ballantyne
Dave Ballantyne
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 8370
Hi ,

The consensus of opinion is that newid() provides a pretty good randomness factor.

So if you select top(100) * from <yourtable> order by newid() , you will get 100 random rows.

If the table has a PK , which i should , you can directly update from that list or wrap that in a CTE and update using that.



Clear Sky SQL
My Blog
n_a_h_k
n_a_h_k
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 10
Hi Dave Ballantyne

Thank you for your answer but i would like know that CTE can be used to update random columns. because when i execute the following statement, i get 100 rows in random. Can i use CTE to update 100 fields in different columns. For example column, i update 10 fields and in column two i update 25 and so on till 100 fields have been changed.

select top 100 from

Table_A
order by newid ()

Thanks a lot
Sad
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search