Solved

insert into Table (select All Mondays between StartDate  and EndDate, numbered)

Posted on 2009-04-06
6
501 Views
Last Modified: 2012-05-06
hi, i need to make events for each Monday (or any day) between StartDate and EndDate, i am clueless


the user will enter startdate and enddate, and i will number each one of them, (not according to week, there will be no events in school holidays, so week10 of the normal calender could be Monday8, if 2 weeks are skipped)

INSERT INTO EVENT(Title,Date,EventNr)
e.g.
Monday 1, 200x/01/01, 1
Monday 2, 200x/01/08, 2
Monday 3, 200x/01/15, 3

please help.
i have looked at this project
http://www.codeproject.com/KB/database/sqlscheduleselector.aspx
but it does not enter any events into databases.

0
Comment
Question by:jxharding
  • 4
  • 2
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 24079508
declare @date1 datetime, @date2 datetime

select @date1 = '1/1/2009', @date2 = '4/6/2009'

set datefirst 1
;with dates(firstdate, lastdate, mondaycount)
as
(
select @date1, @date1, case when datepart(dw, @date1) = 1 then 1 else 0 end
union all
select firstdate, dateadd(d, 1, lastdate), case when datepart(dw,dateadd(d, 1, lastdate) ) = 1 then 1 else 0 end
from dates d
where lastdate <= @date2
)
insert into events(title, date, eventnmbr)
select 'Monday', lastdate, row_number() over(order by lastdate asc) from dates
where mondaycount > 0
0
 

Author Comment

by:jxharding
ID: 24085296
awesome stuff!! i am trying to make head or tails here still.
i am obviously awarding the points, and i see you are nr3 on the hall of fame, so you are busy, but if possible , could you pls save a few seconds on some short questions

1.
set datefirst 1
-is this a declaration and instatiation all in one?
-is it/is it not the same as declare datefirst; set datefirst =1

2.
;with dates
-im trying to google "SQL SERVER WITH" but not getting good results.
am i correct in observing that this is a table declaration and the items in brackets are the column names?
- is this some sort of new way to not work with #tables? (temp tables)

3.
which line of code actually generates the list of dates?

thanks!
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24086286
Hi, no problem...glad it worked for you.  
1.  The set date first made sure that we are both using the same date for the first day of the week. That way when I do this:  datepart(dw,dateadd(d, 1, lastdate) ) = 1, it evaluates to Monday.

2.  This is a common table expression.  Here is an article I wrote about them.  
http://blogs.techrepublic.com.com/datacenter/?p=275

3.  They're all a part of it.  If you look at this:
--this generates the very first line
select @date1, @date1, case when datepart(dw, @date1) = 1 then 1 else 0 end
--this generates all lines after it.
union all
select firstdate, dateadd(d, 1, lastdate), case when datepart(dw,dateadd(d, 1, lastdate) ) = 1 then 1 else 0 end
from dates d
where lastdate <= @date2
--this very last line limits the number so that it only loops up to the date you want.  

Try reading the article I wrote...it might make more sense afterwards.

HTH,
Tim
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:jxharding
ID: 24086333
sometimes great posts like these come along and pinpoint exactly what one needs to learn, thanks a lot!!!
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24086344
You are very welcome!  Glad to help.
Tim
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24086350
BTW..you can check out my new website at www.sqlservernation.com.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now