Click here to Skip to main content
14,540,600 members
Rate this:
Please Sign up or sign in to vote.
See more:
Sir,
I want to select my nth row of my table.
like 3rd row,4th row.......
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try:
WITH myTableWithRows AS (
    SELECT (ROW_NUMBER() OVER (ORDER BY myTable.SomeField)) as row,*
    FROM myTable)
SELECT * FROM myTableWithRows WHERE row = 3
   
Comments
Santhosh Kumar Jayaraman 16-Aug-12 3:49am
   
good one.
Rate this:
Please Sign up or sign in to vote.

Solution 1

you can use row_number function. Refer to following MSDN link ROW_NUMBER (Transact-SQL)[^].
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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;
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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);
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

select * from(
select id,name,row_number() over(order by id) as 'row'  from student)as temp where row=n



Reference
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100