Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the nth row in a SQL Server table? [closed]

Tags:

How would you get the nth row (i.e 5th row) from the result of a query in SQL Server?

like image 246
Kip Birgen Avatar asked Feb 16 '10 14:02

Kip Birgen


People also ask

How do I get nth row in SQL Server?

ROW_NUMBER (Window Function) ROW_NUMBER (Window Function) is a standard way of selecting the nth row of a table. It is supported by all the major databases like MySQL, SQL Server, Oracle, PostgreSQL, SQLite, etc.

How do I get the 5th row in SQL?

For SQL Server, a generic way to go by row number is as such: SET ROWCOUNT @row --@row = the row number you wish to work on.

How do I select every nth row in SQL?

Here's the SQL query to select every nth row in MySQL. mysql> select * from table_name where table_name.id mod n = 0; In the above query, we basically select every row whose id mod n value evaluates to zero.


1 Answers

SQL Server 2005 and newer:

with Records AS(select row_number() over(order by datecreated) as 'row', *                  from Table) select * from records where row=5 

You can change the order by to determine how you sort the data to get the fifth row.

Tested on my local install: Microsoft SQL Server 2005 - 9.00.4053.00 (X64) May 26 2009 14:13:01 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition (64-bit) on Windows NT 6.1 (Build 7600: )

like image 170
JoshBerke Avatar answered Oct 10 '22 20:10

JoshBerke