Solved

Select rows between @StartNum and @EndNum

Posted on 2011-03-19
2
281 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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