rand() in TSQL isn't random at all!

Posted by Dave @ EHS Thu, 14 Sep 2006 15:54:00 GMT

Trying to return a query with a random sort order from TSQL I thought the most obvious
way would be like this:


SELECT *
FROM Northwind..Customers
order by rand()

However for a multi line select statement the value of rand() will be the same for each
row.

This article here has a neat work around with a custom view and a custom function.

However a colleague rightly pointed out that this will do the trick.

SELECT *
ROM Northwind..Customers
order by newid()

 

Thanks Val! 

Comments

Leave a response

Comments