Sunday, August 17, 2008

How to Select random row from a table - Ms Sql Server

One day my boss was asking me to populate data from a table to a temporary table, but he dont want me to popuplate the entire data, he wants me to select some random rows from the table and insert into the table.
so for the requirement i tried and found this...

SELECT * FROM [TableName] ORDER BY NEWID()

NEWID() generates a uniqueindentifier, so when ordering the data by the id it will select some random data

Cheers
Venkat.

No comments: