Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:26:19+00:00 2026-05-31T03:26:19+00:00

I need to fetch a number of rows between two column values, using a

  • 0

I need to fetch a number of rows between two column values, using a defined step value. For example, if the table looks like this:

Id  Name
-----------------------
1   Maria Anders 
2   Christina Berglund
3   Francisco Chang
4   Roland Mendel
5   Diego Roel
6   Eduardo Saavedra
7   Helen Bennett
8   Philip Cramer

and First = 3, Last = 7, Step = 2, query should return:

Id  Name
-----------------------
3   Francisco Chang
5   Diego Roel
7   Helen Bennett

I was thinking of using a modulo to specify which columns should be returned, with something like:

SELECT *
FROM Table 
WHERE (i-3) % 2 = 0

This approach will result in SQL Server iterating through the entire table and calculating the expression for each item. Since I expect to have relatively large step values, I would like to know if there is a strategy which would avoid this (possible using an index to “skip” items).

Is there a better (read: faster) way of doing this? (I am using MS SQL Server 2008 R2)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:26:21+00:00Added an answer on May 31, 2026 at 3:26 am
    DECLARE @start int = 3,  @step int = 2,  @stop int = 7;
    
    ;WITH cte AS
    (
        SELECT @start AS ID
        UNION ALL
        SELECT ID + @step FROM cte WHERE ID + @step <= @stop
    )
    
    SELECT * 
    FROM cte JOIN MyTable M ON cte.ID = MyTable.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to fetch a repeatable random set of rows from a table using
Im using vb.net and I need to fetch data from two different tables and
Here's what I need to fetch: - posts that have comments - number of
I need to run some code that will fetch some configuration values from the
I fetch data in table. but i need to delete it when user click
I have a list of ids of rows to fetch from database. I'm using
I'm using the code below to Fetch a queried set of all rows using
I need to fetch and show data on a webpage whose number of records
i need to fetch a url with javascript/jquery and not php. i've read that
I often need to fetch tgz files, decompress them, and then delete the tgz.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.