Monday, February 18, 2008

Select random from Microsoft SQL Server

I have previously written code to select a random row from a table in SQL Server. Earlier this was a big job with a lot of statements in T-Sql. From versjon 2000 of Sql Server you can use the new future newid() and you will get a random record back. The sql statement will be as follows:

select top 5 * from YourTable order by newid()

that will return 5 random records from the table YourTable.

0 comments: