Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

Select rows between @StartNum and @EndNum

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
johnkainn
Asked:
johnkainn
  • 2
1 Solution
 
Ephraim WangoyaCommented:

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
 
Ephraim WangoyaCommented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now