DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE
SITE SEARCH SQLUSA.com HEADLINES NEWS
SQL E/BOOKS   SQL 2014 PROGRAMMING   DOWNLOADS
SCRIPTS SQL 2005 SQL 2008 ARTICLES
SQL JOBS TWITTER FORMAT VIDEOS
How to sort results randomly?

Execute the following Microsoft SQL Server Transact-SQL (T-SQL) scripts in Management Studio (SSMS) Query Editor, SQLCMD or other client software to randomly sort the result of the query:

USE AdventureWorks2008;

 

-- Random sort with newid()

SELECT ProductNumber FROM Production.Product

ORDER BY NEWID()

 

-- Random sort by checksum newid combination

SELECT ProductNumber FROM Production.Product

ORDER BY CHECKSUM(NEWID())

 

-- Random sort with a not SELECTed unique key / index column

-- It works only if the two columns have no relationship

SELECT ProductNumber FROM Production.Product

ORDER BY Name

 

-- Random sort by uniqueidentifier column

SELECT ProductNumber FROM Production.Product

ORDER BY rowguid

 

-- Random sort using the RAND function

 

SELECT Rnd=RAND(ProductID*ProductID), ProductNumber

FROM Production.Product

ORDER BY Rnd

----------

 

use AdventureWorks;

 

select top (100) Email = EmailAddress

from Person.Contact

order by newid();

------------

 

 

Related article:

http://www.sqlusa.com/bestpractices/newid/

Random Number Machines: A Literature Survey

 

Exam Prep 70-461
Exam 70-461
DATETIME SELECT SELECT INTO DATE PAD STRING DYNAMIC SQL CURSOR MONEY FORMAT PERCENT STORED PROCEDURE SQL SERVER AGENT JOB OPTIMIZATION WHILE LOOP OVER PARTITION BY UPDATE