Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-04-20 : 10:05:23
|
| Lois writes "I have a start date and end date. Each date is passed into a function that returns the first day of the week (ex. 04/08/2004 would return 04/04/2004). This same function does the exact return for the end date.I need to take the start date and add one day to it so it will return not only the start date of 04/04/2004 but then increment the date by 1 day so the next date would be 04/05/2004 and this would go on until it reaches the end date which let's say is 05/02/2004. So my dates would go something like this 04/04/2004, 04/05/2004, 04/06/204, 04/07/2004, etc. I need to put them into a tmp table. My question to you is how do I write a loop that will look at the variable startdate then do the dateadd(d, 1, startdate) and continue until it reached the enddate. I will be placing each incremented date into a tmp table. The table structure I am using only has one date field and I need to break out the date into a day by day listing. Thank you for any assistance." |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-04-20 : 10:29:22
|
| Something Like This:CREATE TABLE #DateTab(DateField DATETIME)DECLARE @StartDate DATETIMEDECLARE @EndDate DATETIMESET @StartDate = '2004-04-01'SET @EndDate = '2004-04-04'WHILE @StartDate <= @EndDateBEGIN INSERT INTO #DateTab SELECT @StartDate SET @StartDate = DATEADD(d, 1, @StartDate)ENDSELECT * FROM #DateTabDuane. |
 |
|
|
cas_o
Posting Yak Master
154 Posts |
Posted - 2004-04-20 : 10:40:33
|
| Use the WHILE loop eg:declare @startdate datetime, @enddate datetime, @middledate datetimeselect @startdate = exec datefunction @startdateselect @enddate = exec datefunction @enddateselect @middledate = dateadd(d,1,@startdate)while @middledate < @endatebegininsert into temptable @middledateselect @middledate = dateadd(d,1,@middledate)end |
 |
|
|
|
|
|
|
|