Solved

Select rows between @StartNum and @EndNum

Posted on 2011-03-19
2
278 Views
Last Modified: 2012-05-11
I have a table MyTable (Id, MyText)
I would like to select all from it and order by MyText.
From that list I would like to select rows between  @StartNum and @EndNum.
For example if @StartNum=20 and @EndNum=30 then I would select the rows between 20 and 30 from an ordered list. How is best to do this?
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35173245

Use CTE
with CTE
AS
  (SELECT Id, MyText, ROW_NUMBER() OVER (ORDER BY MyText) as ROWNO FROM Emp)

select * from CTE where ROWNO >= @StartNum and ROWNO <= @EndNum

Open in new window

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 250 total points
ID: 35173254
Replace emp
with CTE
AS
  (SELECT Id, MyText, ROW_NUMBER() OVER (ORDER BY MyText) as ROWNO FROM MyTable)

select * from CTE where ROWNO >= @StartNum and ROWNO <= @EndNum

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question