In the following article we will go through a solution to a very common requirement of getting Nth record in a SQL server table.
Let us first create a table and add sample data to this table. Col1 in the table is an identity column.
CREATE TABLE #Table1(col1 int, col2 int, col3 char(50))
INSERT INTO #Table1 values (1, 1, 'data value one')
INSERT INTO #Table1 values (2, 1, 'data value two')
Now let us say we want to get 6th row from above table based on values in col1 ascending , then the below query will provide the solution:
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY col1 ASC) AS rownumber, col1, col2,col3
FROM #t1
) AS X
WHERE rownumber = 6
0 Comment(s)