Question Locked

Fetching a random record from SQL Server

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

Regards,
Freelancer

4 replies

by scott_heath

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}

by dinkerbatra

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

select top 1 * from order by newid()

You need to replace the with your table name.

by 93961

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

This discussion is closed to new replies.