Create Date Records

I need to read dates in a table and then create more dates depending on the difference between todays date and date in the record.
Let's say todays date is 2015-10-23.

Table A


If the difference between todays date and the Table A date = 0 do nothing.
The difference between todays date and the second record is 3, so, I need to insert the following records into this table :
The difference between todays date and the second record is 14, so, I need to insert the following records into this table :
All the way to 2015-10-23

Having duplicate dates in the table is no problem. I am going to use this table in a IN statement.
thayduckProgrammer AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
A WHILE loop would work here.   Something like <air code>
Declare @dt_end date = CAST(GETDATE() as date) 
Declare @dt_start = ??? FROM TableA WHERE ?? 
Declare @i int = 0

WHILE @i <= DATEDIFF(day, @dt_start, @dt_end) 
   INSERT INTO ?? (??) VALUES(DATEADD(day, @i, @dt_start)) 
   SET @i = @i + 1

Open in new window

You could also create a SQL Server Calendar Table of days, then this would be a simple matter of...
FROM days
WHERE PKDate >= @dt_start AND PKDate <= @dt_end

Open in new window

Scott PletcherSenior DBACommented:
A "standard" tally table, just a table of sequential numbers, is great for this.  I can't post in-line tally table code myself (filter at work blocks it, thinking it is "sql injection"[!]), but you can find the code on-line to create a tally_table using CTEs:

--WITH (code to create tally_table of sequential numbers from 1 to, say, 10,000)
--INSERT INTO ... ( ... )
SELECT DATEADD(DAY, t.number, a.date_column) --, ...
FROM [Table A] a
INNER JOIN tally_table t ON t.number BETWEEN 1 AND DATEDIFF(DAY, GETDATE(), a.date_column)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
My code to create the tally table is here; this code loads the tally table with 0 thru 1,000,000:
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PortletPaulEE Topic AdvisorCommented:
>>"Having duplicate dates in the table is no problem. I am going to use this table in a IN statement."

That sounds like an approach that might have better options. While generating rows for "missing dates" is quite straight forward, I would prefer to understand what you are actually going to do with them.
thayduckProgrammer AnalystAuthor Commented:
Here is what I did after reading Scott's post on 10/23/2015:

-- Create EscalationDates temp table
      IF Object_id(N'tempdb..#EscalationDates', N'U') IS NOT NULL
        DROP TABLE #EscalationDates

      -- Create temp table
      CREATE TABLE #EscalationDates
           EscalationDates CHAR (10)

      --Write date records that are <= to the latest date that the user can choose from Escalation Days dropdown        
      --Right now, the latest date is 'Next 14 Days', so I will create date records in the TallyDates table that represent the next 14 days from today.
      --If today is 10/05/2015, then I will create date records of 10/05/2015 thru 10/19/2015 in this table.
      --Will use this table later to determine which Note table records to keep.
    ;  WITH TallyDates
           AS (SELECT tDate = Dateadd(DAY, 14, CONVERT(DATE, Getdate())) --latest date user can choose from dropdown        
               UNION ALL
               SELECT tDate = Dateadd(DAY, -1, tDate) --create records that are less then the latest date user can choose from the dropdown    
               FROM   TallyDates
               WHERE  tDate > CONVERT(DATE, Getdate())) -- Stop writing records when you have reached todays date

      --Write records into the #EscalationDates table that represent what the user actually chose from the Escalation Days dropdown.
      --If user chose chose Today, then only put in record from TallyTable that has todays date.
      --If user chose chose Tomorrow, then only put in record from TallyTable that has tomorrows date.
      --If user chose chose Next 7 Days, then only put in records from TallyTable that represent next 7 days from today.
      --If user chose chose Next 14 Days, then only put in records from TallyTable that represent next 14 days from today.

      INSERT INTO #EscalationDates
      SELECT [tDate]
      FROM   TallyDates
             INNER JOIN #EscalationDays
                     ON EscalationCode >= tDate
      WHERE  ( EscalationCode = tDate
               AND EscalationCode = CONVERT(DATE, Getdate()) )
              OR ( EscalationCode = tDate
                   AND EscalationCode = Dateadd(DAY, +1, CONVERT(DATE, Getdate())) )
              OR ( EscalationCode >= tDate
                   AND EscalationCode <> CONVERT(DATE, Getdate())
                   AND EscalationCode <> Dateadd(DAY, +1, CONVERT(DATE, Getdate())) )

Some of the code I found poking around the internet then changed it to work for me.
Some is Scott's.

Now, after building my SQL Notes table from a Oracle a table, I will delete any record in the Notes table that does not have a date that is contained in #EscalationDates table.

I am pretty sure the way I am doing this can be improved, but the final outcome is doing what I need it to do.
thayduckProgrammer AnalystAuthor Commented:
Thanks, your suggestions got me going in the right direction to solve my issue.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.