Fetching a random record from SQL Server

By London Freelancer ·
How do I fetch a random record from SQL Server, I want to display data randomly on a page from the database.

I found the in MYSQL we can use something like order by rand() and get the first record. Do we have something similar in SQL Server

<a href="">Freelancer</a>

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -


by scott_heath In reply to Fetching a random record ...

Get a count of the rows in the table and then generate a random number from 1 to the count and then retrieve that row.

I think it would go something like:

SELECT * FROM {table} WHERE row={rndnumber}

Collapse -


by dinkerbatra In reply to Fetching a random record ...

You can use the following query to get random record SQL Server Database Table;

select top 1 * from <tableName> order by newid()

You need to replace the <tableName> with your table name.

Collapse -

Too Late reply

by Mehul Bhai In reply to Solution

The user must have forgotten his query or may be he has already solved it.

Related Discussions

Related Forums