?
Solved

Stored procedure that increase date for bi-weekly schedule

Posted on 2014-04-24
2
Medium Priority
?
554 Views
Last Modified: 2014-04-25
Hi,

I need to add data into a SQL Server table.
The data to be added is a bi-weekly schedule, that needs to be repeated till the end of the year.

During even weeks the schedule looks like: SCHEDULE 1
Monday          Level1
Tuesday         Level2
Wednesday       Level1
Thursday        Level2

Open in new window


During uneven weeks it is: SCHEDULE 2
Monday         Level1
Tuesday        Level2
Thursday       Level2
Friday         Level1
Saturday       Level2

Open in new window


Data must be entered starting from Tuesday januari 1th 2013 (in week one) current date.

Week 1 will be entered as follows:
01-01-2013  Level2
03-01-2013  Level2
04-01-2013  Level1
05-01-2013  Level2

Week 2 will be entered as follows:
07-01-2013  Level1
08-01-2013  Level2
09-01-2013  Level1
10-01-2013  Level2

Week 3 will be entered as follows:
14-01-2013  Level1
15-01-2013  Level2
17-01-2013  Level2
18-01-2013  Level1
19-01-2013  Level2

etc. etc.

How can this be done in an easy way?
0
Comment
Question by:Delphiwizard
[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
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40022119
This should get you started:
declare @startdate date
declare @enddate date
declare @weeknum int
declare @weekday int
declare @level int

SET @startdate = '2013-01-01'
SET @enddate = '2013-12-31'
WHILE @startdate <= @enddate
BEGIN
    SET @weeknum = DATEPART(ISO_WEEK, @startdate)
    SET @weekday = (DATEPART(dw, @startdate) + @@DATEFIRST - 2) % 7 + 1
    SET @level = 0
    IF (@weeknum % 2 = 0 and @weekday in (1, 3)) or (@weeknum % 2 = 1 and @weekday in (1, 5))
        SET @level = 1
    IF (@weeknum % 2 = 0 and @weekday in (2, 4)) or (@weeknum % 2 = 1 and @weekday in (2, 4, 6))
        SET @level = 2
    --print '--- DEBUG: ' + convert(varchar, @startdate) + ', ' + convert(varchar, @weeknum % 2) + ', ' + convert(varchar, DATEPART(dw, @startdate)) + '/' + convert(varchar, (DATEPART(dw, @startdate) + @@DATEFIRST - 2) % 7 + 1) + ', ' + convert(varchar, @level)
    IF @level > 0
    BEGIN
        PRINT 'date: ' + convert(varchar, @startdate) + ', level: ' + convert(varchar, @level)
        -- place your INSERT statement here...
    END
    SET @startdate = DATEADD(day, 1, @startdate)
END

Open in new window

output:
date: 2013-01-01, level: 2
date: 2013-01-03, level: 2
date: 2013-01-04, level: 1
date: 2013-01-05, level: 2
date: 2013-01-07, level: 1
date: 2013-01-08, level: 2
date: 2013-01-09, level: 1
date: 2013-01-10, level: 2
date: 2013-01-14, level: 1
date: 2013-01-15, level: 2
date: 2013-01-17, level: 2
date: 2013-01-18, level: 1
date: 2013-01-19, level: 2
date: 2013-01-21, level: 1
date: 2013-01-22, level: 2
date: 2013-01-23, level: 1
date: 2013-01-24, level: 2
date: 2013-01-28, level: 1
date: 2013-01-29, level: 2
date: 2013-01-31, level: 2
...
date: 2013-12-23, level: 1
date: 2013-12-24, level: 2
date: 2013-12-25, level: 1
date: 2013-12-26, level: 2
date: 2013-12-30, level: 1
date: 2013-12-31, level: 2

Open in new window

If you can provide some more details about the table and maybe if you want to create a stored procedure that you can call with a year as parameter for example, I can provide some more detailed code if needed.
0
 

Author Closing Comment

by:Delphiwizard
ID: 40022363
Perfect, thank you very much.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

771 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