• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

RAND returns same dataset everytime it's run

I want to extract a random list of X number of customers every month. Here is my query:

SELECT TOP (10) Customers.Name, Customers.Address, Customers.City, Customers.State, Customers.ZipCode, RAND() AS Random
FROM Customers

Microsoft says to leave the seed blank to get a random dataset each time, but this query is getting the same dataset everytime.
0
LezlyPrime
Asked:
LezlyPrime
  • 3
2 Solutions
 
Ephraim WangoyaCommented:

Use this instead
SELECT TOP (10) Customers.Name, Customers.Address, Customers.City, Customers.State, Customers.ZipCode
FROM Customers
order by NEWID()

Open in new window

0
 
Paul JacksonSoftware EngineerCommented:
SELECT TOP (10) newid() as row, Customers.Name, Customers.Address, Customers.City, Customers.State, Customers.ZipCode
FROM Customers
Order By row

http://www.techrepublic.com/article/generate-random-sets-in-sql-server-with-newid/6089823
0
 
Paul JacksonSoftware EngineerCommented:
You want to test what further ? It is also no reason to close the question after perfectly valid solutions have been given.
0
 
Paul JacksonSoftware EngineerCommented:
Microsofts own documentation says that successive calls to rand() with the same seed produces the same results.

http://msdn.microsoft.com/en-us/library/ms177610.aspx
0
 
LezlyPrimeAuthor Commented:
Before logging off last night I ran the query without any of the RAND stuff & got the same results & thought, "I'm way off base here." I was right.

Great answers & both worked. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now