Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Query for geeting rows from middle of a table

Author  Topic 

alto
Starting Member

3 Posts

Posted - 2008-08-17 : 10:09:22
I need help for a query, Suppose in a table there are a million data. I need to select rows such as row number 5 to 13. Plz give the SQL quey for SQL server 2005.

ayamas
Aged Yak Warrior

552 Posts

Posted - 2008-08-17 : 10:35:26
DECLARE @Sample TABLE
(id int identity(1,1),names varchar(3))
insert into @sample
select 'a' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'e' union all
select 'f'




select * from
(
select row_number() over(order by id) as rowid,names from @sample
)t
where rowid between 2 and 5
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2008-08-17 : 10:46:42
Bear in mind that SQL has no concept of row ordering without an order by of some form. So, if you want the 5th row, you need to know the 5th row when ordered by what.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-17 : 12:47:19
SELECT TOP 9 * FROM (SELECT TOP 13 * FROM Table1 ORDER BY Col1) AS d ORDER BY Col1 DESC




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -