Retrieving Random records from a result set in sql server [Resolved]

Posted by Sureshij under Sql Server on 9/24/2013 | Points: 10 | Views : 1304 | Status : [Member] | Replies : 1
Hi All,

How to retrieve random records from a result set in SQL Server....
For example, If i have following data
ID
---
167
185
187
147
121
103
175
115
158
196

I want to retrieve 5 records randomly from the above records using SQL Server....

ij


Responses

Posted by: Bandi on: 9/24/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Hi Suresh,

Use TOP 5 clause for getting 5 records from the result set....
To get random records use the newid() function in ORDER BY clause....


SELECT TOP 5 ID
FROM TableName
ORDER BY newid();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sureshij, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response