Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Not using loops - to loop
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

United Kingdom
68 Posts

Posted - 05/04/2013 :  15:14:32  Show Profile  Visit richardlaw's Homepage  Reply with Quote

I have a table of classes. Each class runs only once a week, and the classes table stores the day it runs as an INT (1 = Mon, 2 = Tue, … 7 = Sun).

I now need to create a temp table that list all the classes that take place within a date range (like a calendar of events). So for example, if the first class runs even Monday, and my date range is over 4 weeks and 3 days, I would expect to see in my temp table 5 rows (date, classID) – assuming one of the 3 days is a Monday.

I assume I would need a loop of some kind that filters each date in the date range to check if a class runs on that day, if so, add a row to the temp table with the date and classID; if not, move on to the next class before moving on to the next day (date + 1 day).

I’d really like to find a way that does not require loops. The method above seems very process intensive. I’m new to TSQL, but looping like that just feels clumsy.

Any ideas?
Thanks as always

Aged Yak Warrior

549 Posts

Posted - 05/04/2013 :  23:06:53  Show Profile  Reply with Quote
Refer to &
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 05/06/2013 :  02:03:12  Show Profile  Reply with Quote
The best thing in your case might be to use a calendar table as base and then join to your table based on the dates. Then you can filter on day field to give you only mondays


SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000