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
 General SQL Server Forums
 New to SQL Server Programming
 Define Start index and Count in SELECT statement

Author  Topic 

shashwat2691
Starting Member

11 Posts

Posted - 2011-06-09 : 03:43:28
Can I define range in SELECT Statement.

Exmpl:
Suppose a select query return 100 records. Now I want to select only 20 records from them starting from 30 to 50.

can i do so in SELECT statement.

Shashwat Tripathi - INDIA

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-09 : 04:30:21
If you are on SQL Server 2005 or above then you use ROW_NUMBER().


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2011-06-09 : 04:35:19
If your Query have uniqe serial no u can do like this

Select * from my_table where id>=30 -- Starting No
and id<=30+20 -- No. of records


If you does't have unique serial no on ur result set then.. ur approach like this...

Select * from (
Select ROW_NUMBER() over(order by id) as s_no,* from my_table )
a where s_no >=30
and s_no<=30+20

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page
   

- Advertisement -