Randomize Recordset Using SQL

SQLServer

I have read articles in the past that show how to randomize a ColdFusion query once it’s been retrieved from the database. There are probably times when there is a need to do this. However, a more efficient way is to allow the database to handle it before it is even handed to ColdFusion.

Here is an example using SQL Server:


Select
    adID, adLink, adImage, newid() as randomNumber
From
    Ads
Order by
    randomNumber

Short and simple

:)

UPDATED 2/19/2008

See Jason’s note in the comments saying you can change it to the following:

<b>
Select
    adID, adLink, adImage
From
    Ads
Order by
    newid()</b>

2 Responses to “Randomize Recordset Using SQL”

  1. Jason Says:

    I’m sure you kow this but you could shorten it even more by just ordering by newid()

    SELECT adID, adLink, adImage, FROM Ads ORDER BY newid()

  2. Vincent Collins Says:

    Thanks Jason,

    Actually, I didn’t know that thanks! :)

    Has that always been a feature?

Leave a Reply