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 8117231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:05:57+00:00 2026-06-06T04:05:57+00:00

The title doesn’t quite capture what I mean, and this may be a duplicate.

  • 0

The title doesn’t quite capture what I mean, and this may be a duplicate.

Here’s the long version: given a guest’s name, their registration date, and their checkout date, how do I generate one row for each day that they were a guest?

Ex: Bob checks in 7/14 and leaves 7/17. I want

('Bob', 7/14), ('Bob', 7/15), ('Bob', 7/16), ('Bob', 7/17) 

as my result.

Thanks!

  • 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-06-06T04:05:58+00:00Added an answer on June 6, 2026 at 4:05 am

    I would argue that for this specific purpose the below query is about as efficient as using a dedicated lookup table.

    DECLARE @start DATE, @end DATE;
    SELECT @start = '20110714', @end = '20110717';
    
    ;WITH n AS 
    (
      SELECT TOP (DATEDIFF(DAY, @start, @end) + 1) 
        n = ROW_NUMBER() OVER (ORDER BY [object_id])
      FROM sys.all_objects
    )
    SELECT 'Bob', DATEADD(DAY, n-1, @start)
    FROM n;
    

    Results:

    Bob     2011-07-14
    Bob     2011-07-15
    Bob     2011-07-16
    Bob     2011-07-17
    

    Presumably you’ll need this as a set, not for a single member, so here is a way to adapt this technique:

    DECLARE @t TABLE
    (
        Member NVARCHAR(32), 
        RegistrationDate DATE, 
        CheckoutDate DATE
    );
    
    INSERT @t SELECT N'Bob', '20110714', '20110717'
    UNION ALL SELECT N'Sam', '20110712', '20110715'
    UNION ALL SELECT N'Jim', '20110716', '20110719';
    
    ;WITH [range](d,s) AS 
    (
      SELECT DATEDIFF(DAY, MIN(RegistrationDate), MAX(CheckoutDate))+1,
        MIN(RegistrationDate)
        FROM @t -- WHERE ?
    ),
    n(d) AS
    (
      SELECT DATEADD(DAY, n-1, (SELECT MIN(s) FROM [range]))
      FROM (SELECT ROW_NUMBER() OVER (ORDER BY [object_id])
      FROM sys.all_objects) AS s(n)
      WHERE n <= (SELECT MAX(d) FROM [range])
    )
    SELECT t.Member, n.d
    FROM n CROSS JOIN @t AS t
    WHERE n.d BETWEEN t.RegistrationDate AND t.CheckoutDate;
    ----------^^^^^^^ not many cases where I'd advocate between!
    

    Results:

    Member    d
    --------  ----------
    Bob       2011-07-14
    Bob       2011-07-15
    Bob       2011-07-16
    Bob       2011-07-17
    Sam       2011-07-12
    Sam       2011-07-13
    Sam       2011-07-14
    Sam       2011-07-15
    Jim       2011-07-16
    Jim       2011-07-17
    Jim       2011-07-18
    Jim       2011-07-19
    

    As @Dems pointed out, this could be simplified to:

    ;WITH natural AS 
    (
      SELECT ROW_NUMBER() OVER (ORDER BY [object_id]) - 1 AS val 
      FROM sys.all_objects
    ) 
    SELECT t.Member, d = DATEADD(DAY, natural.val, t.RegistrationDate) 
      FROM @t AS t INNER JOIN natural 
      ON natural.val <= DATEDIFF(DAY, t.RegistrationDate, t.CheckoutDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the title doesn't capture what I'm trying to ask but here is
The title of this doesn't quite make sense, so I'll do my best to
I'm sorry if this title doesn't describe the problem properly but I wasn't sure
I'm not too great with jquery and maybe this title doesn't explain too well
I hope the title doesn't sound too subjective; I absolutely do not mean to
I am sure the title of my question doesn't make much sense. But here
The title doesn't describe this very well, but assuming the following simplified table in
Ok I know the title doesn't fully explain this question. So I'm writing a
Firstly, sorry if my title doesn't quite explain the situation, I had problems thinking
The title probably doesn't make much sense, so I'll try to be descriptive here

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.