| | |
Loop to generate future dates?!
Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved |
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)
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.
1
#2 Oct 27th, 2009
MS SQL Syntax (Toggle Plain Text)
IF OBJECT_ID('tempdb..#DateTable', 'U') IS NOT NULL DROP TABLE #DateTable CREATE TABLE #DateTable ( [Date] DateTime ) DECLARE @Begin DateTime, @End DateTime, @dt DateTime SET @Begin = Cast(Floor(Cast(GetDate() AS float)) AS datetime) SET @End = Cast('12/31/2020' AS DateTime) SET @dt = @Begin SET NOCOUNT ON WHILE (@dt <= @End) BEGIN INSERT INTO #DateTable ([Date]) Values (@dt) SET @dt = DateAdd(month, 6, @dt) END SET NOCOUNT OFF SELECT * FROM #DateTable
Results:
text Syntax (Toggle Plain Text)
Date ----------------------- 2009-10-27 00:00:00.000 2010-04-27 00:00:00.000 2010-10-27 00:00:00.000 2011-04-27 00:00:00.000 2011-10-27 00:00:00.000 2012-04-27 00:00:00.000 2012-10-27 00:00:00.000 2013-04-27 00:00:00.000 2013-10-27 00:00:00.000 2014-04-27 00:00:00.000 2014-10-27 00:00:00.000 2015-04-27 00:00:00.000 2015-10-27 00:00:00.000 2016-04-27 00:00:00.000 2016-10-27 00:00:00.000 2017-04-27 00:00:00.000 2017-10-27 00:00:00.000 2018-04-27 00:00:00.000 2018-10-27 00:00:00.000 2019-04-27 00:00:00.000 2019-10-27 00:00:00.000 2020-04-27 00:00:00.000 2020-10-27 00:00:00.000 (23 row(s) affected)
0
#3 Oct 27th, 2009
•
•
•
•
MS SQL Syntax (Toggle Plain Text)
IF OBJECT_ID('tempdb..#DateTable', 'U') IS NOT NULL DROP TABLE #DateTable CREATE TABLE #DateTable ( [Date] DateTime ) ...... ...... ......
![]() |
Similar Threads
- how to print the timetable in database (MS SQL)
- Generate Random numbers without duplication other than storing in the database (Visual Basic 4 / 5 / 6)
- How to generate data reports using two DTPicker's (Visual Basic 4 / 5 / 6)
- How to generate an array using a loop that returns one single value each time? (Java)
- modify a do while loop (Visual Basic 4 / 5 / 6)
- send data from while loop to mysql database (PHP)
- classes help (C++)
- Element is undefined in a Java object of type class [Ljava.lang.String; referenced as (ColdFusion)
- 2038 Problem (C++)
- Creating pages and such (ASP.NET)
Other Threads in the MS SQL Forum
- Previous Thread: Getting sum of cost from one table where quantity on another table
- Next Thread: Sql Query
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last autogrowth business connectingtodatabaseinuse count cursor data database dateadd datepart day" dbsize deadlock delete_trigger getdate highperformancecomputing hpc hpcserver2008 ibm iis loop maximum microsoft ms mssql multiple multithreading news number permission query reporting result server services sets source sql sqlserver sqlserver2005 supercomputing tables uniqueid update view weekday






