Brian Clifton . com

Randomizing your selection in SQL Server

Posted by Brian Clifton
Written May 28, 2008 at 00:33
Sometimes you might want to randomize your dataset. In the SQL driven MP3 player I worked on, I wrote a stored procedure to handle putting together a playlist for the user (since all the song data is already in the database). There are probably a lot of other good uses for random selections and the code sample below works great.

In your select clause you can add an ORDER BY statement and use NEWID().
SELECT * FROM
TableName
ORDER BY NEWID()

 

© Brian Clifton. All rights reserved.