Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I store and query schedule data?
    primarykey
    data
    text
    <p>I'd like to allow my users to setup a schedule for their events. It could be a single day, or for convenience I'd like to allow them to specify a reoccurring event (similar to an Outlook appointment).</p> <p>For the single event it seems pretty easy (pseudo-code):</p> <p>Just have a DateOfEvent column that has the date on it.</p> <p>To grab future events:</p> <p>Select * from events where DateOfEvent > {DateTime.Now}</p> <p>But how could I store and query a reoccurring event? I don't need to do times, as I'd just store that seperately, and if they needed a different time I'd just have them create another event. So no: Every wednesday at 5 and thursdays at 3.</p> <p>Examples:</p> <p>Every mon, tues, wed, thu, fri, every week</p> <p>Every wed every week</p> <p>Every second tuesday of the month</p> <p><strong>What I used to query</strong></p> <p>I added a start and end dates. If the user selected a single date I set both start and end dates to the chosen date. I just had to modify the answer's code a bit.</p> <pre><code>DECLARE @StartDate SMALLDATETIME, @EndDate SMALLDATETIME; SELECT @StartDate = '20091129', @EndDate = '20101220'; SELECT d.CurrentDate, m.* FROM Calendar AS d INNER JOIN Meet AS m ON ( (d.CurrentDate = m.StartDate AND d.CurrentDate = m.EndDate) OR d.DaysOfTheMonth = m.DayOfTheMonth OR (d.DaysOfTheWeek = m.DayOfTheWeek AND COALESCE(m.WeekOfTheMonth, d.WeekOfTheMonth) = d.WeekOfTheMonth) OR d.DaysOfTheWeek IN (1,7) AND m.OnWeekends = 1 OR d.DaysOfTheWeek BETWEEN 2 AND 6 AND m.OnWeekDays = 1 ) -- now you can inner join to the event table -- to list the name, other details, etc. WHERE d.CurrentDate BETWEEN @StartDate AND @EndDate AND d.CurrentDate BETWEEN m.StartDate AND m.EndDate ORDER BY d.CurrentDate; GO </code></pre> <p>Filling in the Calandar table:</p> <pre><code>INSERT dbo.Calendar(CurrentDate, DaysOfTheMonth, DaysOfTheWeek, WeekOfTheMonth, IsWeekDay, Even) SELECT CurrentDate, DaysOfTheMonth, DaysOfTheWeek, WeekOfTheMonth = DATEDIFF(WEEK, DATEADD(DAY, 1-DaysOfTheMonth, CurrentDate), CurrentDate) + 1, CASE WHEN DaysOfTheWeek IN (1, 7) THEN 0 ELSE 1 END AS IsWeekDay, CASE WHEN w % 2 = 1 THEN 0 ELSE 1 END AS Even FROM ( SELECT CurrentDate, DaysOfTheMonth = DATEPART(DAY, CurrentDate), DaysOfTheWeek = DATEPART(WEEKDAY, CurrentDate), w = DATEPART(WEEK, CurrentDate) FROM ( SELECT CurrentDate = DATEADD(DAY, n-1, '20141001') FROM ( SELECT TOP 900 n = ROW_NUMBER() OVER (ORDER BY c1.[object_id]) FROM sys.all_columns AS c1 CROSS JOIN sys.all_columns AS c2 ORDER BY n ) AS x) AS y) AS z; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload