Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-04-06
6
503 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spx for moving values to new table 5 74
SQL Query 2 63
Testing connection to sql 7 61
SQL Server 2005 - Comparing Fields' Contents 6 44
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

860 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