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

  • SEARCH
  • Home
  • 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 258533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:11:18+00:00 2026-05-11T22:11:18+00:00

This may not be possible, but I thought I’d throw it out here: Given

  • 0

This may not be possible, but I thought I’d throw it out here:

Given the following table:

ID, Begin, End
123, 1, N

Where N is an integer, write a query to return the following result set:

ID, Begin, End
123, 1, 1
123, 1, 2
123, 1, 3
.
.
.
123, 1, N

The platform we are using is SQL Server 2005, but if you can do it with another flavor of SQL, I’d still be interested in the solution.

  • 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-11T22:11:19+00:00Added an answer on May 11, 2026 at 10:11 pm

    try this:

    create table #smalltable (id int, [begin] int, [end] int)
    insert into #smalltable values (123,1,4)
    insert into #smalltable values (124,1,12)
    insert into #smalltable values (125,1,7)
    
    ;WITH digits (d) AS (
        SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION
        SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
        SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION
        SELECT 0)
    SELECT
        s.id, s.[begin], n.Number AS [End]
        FROM (SELECT i.d + ii.d * 10 + iii.d * 100 + iv.d * 1000 +
                  v.d * 10000 + vi.d * 100000 AS Number
                  FROM digits            i
                      CROSS JOIN digits  ii
                      CROSS JOIN digits  iii
                      CROSS JOIN digits  iv
                      CROSS JOIN digits  v
                      CROSS JOIN digits  vi
             ) AS N
            INNER JOIN #smalltable                                    s  ON 1=1
            INNER JOIN (SELECT MAX([end]) AS MaxEnd FROM #smalltable) dt ON 1=1
       WHERE n.Number > 0 AND n.Number<=dt.MaxEnd
        AND n.Number<=s.[end]
       ORDER BY s.id,n.Number
    

    comments

    • don’t name your columns the reserved words: “begin” and “end”, you’ll thank me some day.
    • if you plan to run this in production many times, create a Numbers table
      and use this query instead:

    have to have a table Numbers before this will work (see link above)

    SELECT
        s.id,s.[begin],n.Number AS [End]
        FROM Numbers                n
            INNER JOIN #smalltable  s ON 1=1
       WHERE  n.Number > 0 AND n.Number<=s.[end]
       ORDER BY s.id,number
    

    it will run better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I may be a victim of type erasure but thought I'd check
This is a bit of a vague notion which I have been running over
Wikipedia: Directed Acyclic Graph Not sure if leaf node is still proper terminology since
I'm inserting records from one sql server db into another. I need to get
I'm using Solr for searching, and recently started using faceting to allow users to
I am looking for a way to embed a blog engine into my own
I have been testing the PS3 browser (NetFront) with embedded Flash components on a
If you want to move your development process from Test-Driven Development to Behavior-Driven Development
I'm using the regex System.Text.RegularExpressions.Regex.Replace(stringToSplit, ([A-Z]), $1).Trim() to split strings by capital letter, for

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.