Solved

Select rows between @StartNum and @EndNum

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Maintenance Plan 3 30
Sql query 34 35
Excel conversion issue with Sql server 14 47
SQL compatability in SQL 2016 2 31
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…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

17 Experts available now in Live!

Get 1:1 Help Now