Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Sir,
I want to select my nth row of my table.
like 3rd row,4th row.......
Posted

Try:
WITH myTableWithRows AS (
    SELECT (ROW_NUMBER() OVER (ORDER BY myTable.SomeField)) as row,*
    FROM myTable)
SELECT * FROM myTableWithRows WHERE row = 3
 
Share this answer
 
Comments
Santhosh Kumar Jayaraman 16-Aug-12 3:49am    
good one.
you can use row_number function. Refer to following MSDN link ROW_NUMBER (Transact-SQL)[^].
 
Share this answer
 
for example, to select 5th row

SELECT TOP 1 T.* FROM
(SELECT TOP 5 * FROM DBT_Person ORDER BY AGE ASC) AS T
ORDER BY T.AGE DESC;
 
Share this answer
 
If you could, uses LINQ to SQL. That kind of stuff is much easier with Linq To SQL.

var result = (
  from m in dataContext.theTable
  select m).Skip(50).Take(1);
 
Share this answer
 
SQL
select * from(
select id,name,row_number() over(order by id) as 'row'  from student)as temp where row=n



Reference
 
Share this answer
 
v2
Comments
Member 12635513 14-Jul-16 12:40pm    
This solution helped me tremendously 2008 sql server. Top N wasn't working due to poor data quality. Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900