I have two tables I am working with and trying to pouplate a third table with dates based on certain days of the week. To explain better here is an example of the two primary table structures:Table = Class (has the start and finish dates of a class)ID, StartDate, FinishDateTable = Times (Lists the times of the class by the day of the week)ID, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, SaturdaySo, for example, if the Class table contains this data:ID = 123StartDate = '2005-06-01'FinishDate = '2005-06-11'and the Times table contains this data:ID = 123 Sunday = NULLMonday = NULLTuesday = NULLWednesday = NULLThursday = NULLFriday = 6:00Saturday = 2:00Then I want a table @dates to be populated with all of the dates of the Classes so it would look like this (which basically just lists the dates for the Friday and Saturday classes that fall between the start and finish date)Classdate 2005-06-03 2005-06-042005-06-10 2005-06-11To try an do this I saw a previous posting on this site and put together this queryset nocount ondeclare @dt datetime,@from datetime, @to datetime, @Day_to_get varchar(20)set @from = (Select StartDate from [Class] where ID = 123)set @to = (Select FinishDate from [Class] where ID = 123)set @Day_to_get = 'Friday' declare @dates table ([dates] datetime)Select @dt =casewhen datename(weekday,@from) = @Day_to_get then @fromwhen datename(weekday,dateAdd(day,1,@from)) = @Day_to_get then dateAdd(day, 1, @from)when datename(weekday,dateAdd(day,2,@from)) = @Day_to_get then dateAdd(day, 2, @from)when datename(weekday,dateAdd(day,3,@from)) = @Day_to_get then dateAdd(day, 3, @from)when datename(weekday,dateAdd(day,4,@from)) = @Day_to_get then dateAdd(day, 4, @from)when datename(weekday,dateAdd(day,5,@from)) = @Day_to_get then dateAdd(day, 5, @from)when datename(weekday,dateAdd(day,6,@from)) = @Day_to_get then dateAdd(day, 6, @from) end While @dt <= @toBegin insert @dates values (@dt) Select @dt = dateadd(day,7,@dt)EndSelect * from @dates
My question is on the "set @Day_to_get" part I would like it to determine from the Times table which days are not NULL and then loop through all of those days in this query instead of me having to put in each day. So in this case it would add all of the Friday dates and all of the Saturday dates to the @dates table? Any ideas on how to do this?