Getting Random Values in TSQL

After a bit of research, finally found the answer to how to set up random values for each record in SQL Server:

rand( CAST( NewID() AS varbinary) )

In context:

SELECT rand( CAST( NewID() AS varbinary) ) FROM table_name

The reasoning is this:

  • Using rand() doesn’t work because it is executed per-query, so you end up with value being the same for all columns
  • Using NewID() doesn’t work for me because I need a numeric value
  • Using record ID doesn’t work because the result was not random enough…  all values were uniformly ordered…  seriously…

So I new it had to be something like rand( NewID() ) to get something that looks random…  and I found the answer here.

Advertisements

Leave a comment

Filed under Tech

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s