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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Dates based on day of week

Author  Topic 

mlmeyer
Starting Member

26 Posts

Posted - 2005-05-26 : 11:48:37
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, FinishDate

Table = Times (Lists the times of the class by the day of the week)
ID, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

So, for example, if the Class table contains this data:
ID = 123
StartDate = '2005-06-01'
FinishDate = '2005-06-11'

and the Times table contains this data:
ID = 123
Sunday = NULL
Monday = NULL
Tuesday = NULL
Wednesday = NULL
Thursday = NULL
Friday = 6:00
Saturday = 2:00

Then 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-04
2005-06-10
2005-06-11

To try an do this I saw a previous posting on this site and put together this query

set nocount on
declare @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 =
case
when datename(weekday,@from) = @Day_to_get then @from
when 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 <= @to
Begin
insert @dates values (@dt)
Select @dt = dateadd(day,7,@dt)
End
Select * 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?

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2005-05-26 : 12:22:47
I'd recommend the following approach:

1. Create a 'days' table with an ID that corresponds to the number that is returned by the DATEPART function when supplied with 'weekday' as determined by the value set by SET DATEFIRST (see BOL).

2. Normalise your 'times' table to remove the repeating groups. I.e.:
CREATE TABLE dbo.Times 
(
ClassID INT,
DayID TINYINT
)

3. Create a table of consecutive numbers starting at 0 up to a value equivalent to the maximum number of days any course is likely to run for (and then some!).

This will allow you to return your set of dates for a particular course without any looping, something like this:

SELECT
c.ClassID,
DATEADD(day, n.number, c.startdate)
FROM Class AS c
join Numbers AS n
ON n.number <= DATEDIFF(day, c.startdate, c.FinishDate)
JOIN Times AS t
ON t.dayid = DATEPART(weekday, DATEADD(day, n.number, c.startdate))
WHERE
c.ClassID = @ClassID




Mark
Go to Top of Page

mlmeyer
Starting Member

26 Posts

Posted - 2005-05-26 : 13:47:57
I just tried your approach and it worked great with a lot less code. Thanks!
Go to Top of Page

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2005-05-27 : 05:15:55
No problem

Mark
Go to Top of Page
   

- Advertisement -