Categories
loops sql sql-server sql-server-2005 while-loop

sql while loop with date counter

I need to do something like this in sql:

declare @StartDate varchar(10)
declare @EndDate varchar(10)
set @StartDate="12/31/2008"
set @EndDate="1/11/2009"
Declare @date varchar = @StartDate
while (@date <= @EndDate)
begin
-- some statements
set @date += 1 -- basically increment by 1 day
end

How can I do the above correctly in SQL? Basically, my startdate and enddate are strings and not datetimes because my business logic is referencing string column in another table with a date as the name of the column-But I need to loop through a bunch of columns, each column having the name of the next day’s date.

If the date is 11/07/2009, the name of the column would be ’11/7/2009′ (without the 0 in the 7), so I have to watch out for that too.

Any help is appreciated!

Thanks.