Tuesday, April 22, 2008

Random numbers in SQL Server

I'm putting this here for my future reference.

I sometimes need to generate random numbers (especially when creating test data in a table). Here's a method to generate random values that will work even when used in a set-based action (ie. inserting records into table B using table A as the source).

select RAND(CHECKSUM(NEWID())) * [upper limit]

where [upper limit] is the maximum number you want in your random set.

No comments: