Loop to generate future dates?!

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 28
Reputation: cheapterp is an unknown quantity at this point 
Solved Threads: 2
cheapterp's Avatar
cheapterp cheapterp is offline Offline
Light Poster

Loop to generate future dates?!

 
0
  #1
Oct 26th, 2009
What is the best way to generate a set of dates based on today's date? Essentially, what I wanna do is:
1. Get Current Date
2. Populate a table with future dates 6 months apart and go up until I reach, let's say, 12/31/2020

I have looked at some of the date functions offered by MS-SQL 2005, but not quite sure how to run a loop. (I use ColdFusion as the front-end)
Last edited by cheapterp; Oct 26th, 2009 at 1:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,224
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
Oct 27th, 2009
  1. IF OBJECT_ID('tempdb..#DateTable', 'U') IS NOT NULL DROP TABLE #DateTable
  2. CREATE TABLE #DateTable
  3. (
  4. [Date] DateTime
  5. )
  6.  
  7. DECLARE @Begin DateTime, @End DateTime, @dt DateTime
  8.  
  9. SET @Begin = Cast(Floor(Cast(GetDate() AS float)) AS datetime)
  10. SET @End = Cast('12/31/2020' AS DateTime)
  11. SET @dt = @Begin
  12.  
  13. SET NOCOUNT ON
  14. WHILE (@dt <= @End)
  15. BEGIN
  16. INSERT INTO #DateTable ([Date]) Values (@dt)
  17. SET @dt = DateAdd(month, 6, @dt)
  18. END
  19. SET NOCOUNT OFF
  20.  
  21. SELECT * FROM #DateTable

Results:
  1. Date
  2. -----------------------
  3. 2009-10-27 00:00:00.000
  4. 2010-04-27 00:00:00.000
  5. 2010-10-27 00:00:00.000
  6. 2011-04-27 00:00:00.000
  7. 2011-10-27 00:00:00.000
  8. 2012-04-27 00:00:00.000
  9. 2012-10-27 00:00:00.000
  10. 2013-04-27 00:00:00.000
  11. 2013-10-27 00:00:00.000
  12. 2014-04-27 00:00:00.000
  13. 2014-10-27 00:00:00.000
  14. 2015-04-27 00:00:00.000
  15. 2015-10-27 00:00:00.000
  16. 2016-04-27 00:00:00.000
  17. 2016-10-27 00:00:00.000
  18. 2017-04-27 00:00:00.000
  19. 2017-10-27 00:00:00.000
  20. 2018-04-27 00:00:00.000
  21. 2018-10-27 00:00:00.000
  22. 2019-04-27 00:00:00.000
  23. 2019-10-27 00:00:00.000
  24. 2020-04-27 00:00:00.000
  25. 2020-10-27 00:00:00.000
  26.  
  27. (23 row(s) affected)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 28
Reputation: cheapterp is an unknown quantity at this point 
Solved Threads: 2
cheapterp's Avatar
cheapterp cheapterp is offline Offline
Light Poster
 
0
  #3
Oct 27th, 2009
Originally Posted by sknake View Post
  1. IF OBJECT_ID('tempdb..#DateTable', 'U') IS NOT NULL DROP TABLE #DateTable
  2. CREATE TABLE #DateTable
  3. (
  4. [Date] DateTime
  5. )
  6. ......
  7. ......
  8. ......
Thanks Scott, that was just what I was looking for. This was my first time working with loops in MS-SQL - very useful!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC