Solved

Select rows between @StartNum and @EndNum

Posted on 2011-03-19
2
274 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
  • 2
2 Comments
 
LVL 32

Expert Comment

by:ewangoya
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:
ewangoya 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now