Question

Locked

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

Regards,
<a href="http://www.codeguru.services.officelive.com/default.aspx">Freelancer</a>

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Well...

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 -

Solution

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.

Back to Networks Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums