Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-04-06
6
Medium Priority
?
509 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
[X]
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
  • 4
  • 2
6 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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 …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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