Possible Duplicate:
Select n random rows from SQL Server table
I have database and table like that:
Persons
id Name
-- -----
1 Jim
2 John
3 Jaimy
I want to pick one row in the result randomly. Is there any function in SQL Server?
Possible Duplicate:
Select n random rows from SQL Server table
I have database and table like that:
Persons
id Name
-- -----
1 Jim
2 John
3 Jaimy
I want to pick one row in the result randomly. Is there any function in SQL Server?
This might do it for you -
SELECT TOP 1 id, Name
FROM Persons
ORDER BY NEWID()
The Random purists won't like it. It works by generating a new GUID for each row.
It might also hurt performance if you have a lot of rows in the Persons
table. Ideally you'd want to find a way to minimise the number of rows that you run that over first.
There is a more detailed answer in this question that might answer the question fully.
You could use something like:
SELECT TOP 1 id, Name
FROM Persons
ORDER BY NEWID()