Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update random numbers for top 100 rows in sql?

I need to update random numbers for top 100 rows (a field) in sql. random number should be less than 100. how to do that?

like image 257
James123 Avatar asked May 07 '11 15:05

James123


1 Answers

In SQL 2008

update top (100) MyTable
set MyField = cast(cast(crypt_gen_random(1) as int) * 100.0 / 256 as int)

I believe the same will work in SQL 2005.

[Edit]

If it doesn't work in SQL 2005, you can do this:

update top (100) MyTable
set MyField = abs(cast(newid() as binary(6)) % 100)
like image 94
Alex Aza Avatar answered Oct 30 '22 23:10

Alex Aza