Solved

Find date record in calendar table based on Start Date and # of Days to add

Posted on 2011-09-27
7
329 Views
Last Modified: 2012-05-12
I have a calendar table with calendar.calendardays (every day of the year), calendar.Weekdays (Monday - Sunday) and calendar.OfficeWorkdays (states whether it is a workday, holiday, weekend or closure).

I have a delphi form where user inputs the Start Date and # of Days in Mfg.  What I would like to happen on EditValueChange is to calculate the End Date based on (Start Date + # of Days in Mfg) BUT it should only add days that are marked as 'Workday' in the Calendar.OfficeWorkdays field.

For example, start date on my test record is 11/15/11. When I enter 2, the result should be 11/16/11 BECAUSE the Start Date is INCLUDED in the 2 days. Another example with the same start date of 11/15 and user enters in 5, resulting date should be 11/21/11. 15th is day 1, 16th is day 2, 17th is day 3, 18th is day 4, SKIPS 19th and 20th because they are in Calendar.OfficeWorkdays as 'Weekend' and the 21st is the next 'Workday' and that is day 5.

Below code is my attempt... but as soon as I hit a non-Workday, I get incorrect results. What I tried to do is get the count of non-Workdays, get the start date and get the # of days to add, then ADD that all together but what happens is I end up on a Sunday. I also have a CalendarView I was going to test out and it is simply the Calendar table but filtering only for OfficeWorkdays = 'Workday'.

Thank you for your help!
procedure TfrmMfgScheduleForm.edMfgDaysPropertiesEditValueChanged(
  Sender: TObject);
var sMfgStart, sDays, sSql, sDaysToAdd, sMfgComplete : String;  dMfgStart, dDays, dMfgComplete, dDaysToAdd :TDateTime;
        fDayToAdd : Variant;
begin
  inherited;

  dMfgStart     := StrToDate(edMfgStart.Text);
//  dMfgComplete  := StrToDate(edMfgTarget.Text);
  fDayToAdd     := StrToFloat(edMfgDays.Text);
//  dDays         := StrToDate(DateToStr(dMfgStart + StrToFloat(edMfgDays.Text)));
  dDays         := dMfgStart + fDayToAdd;

  //how many non-workdays are there between start of mfg plus days to add?
  with dmMfgSchedule.qrySearch do begin
        Close;
        sql.Clear;
//        sql.Add('SELECT CalendarDays FROM CalendarView WHERE CalendarDays = ('''+DateToStr(dDays)+''') ');
        sql.Add('SELECT COUNT(ID) AS Expr1 FROM Calendar ');
        sql.Add('WHERE (CalendarDays BETWEEN '''+DateToStr(dMfgStart)+''' AND '''+DateToStr(dDays)+''') ');
        sql.Add('AND OfficeWorkDays <> ''Workday'' ');
        Open;
        end;
        sDaysToAdd      := dmMfgSchedule.qrySearch.FieldValues['CalendarDays'];
        sMfgComplete    := DateToStr( (fDayToAdd - 1) + (dMfgStart) + (StrToFloat(sDaysToAdd)) );

  //Calculate Mfg Start plus Days in Mfg
  sSql := ' UPDATE PCJobData SET Mfg_Complete = '''+DateToStr(dMfgComplete)+''' ';
  sSql := sSql + ' WHERE ID = '''+sJobID+''' ' ;
  dmMfgSchedule.ADOCommand1.CommandText:= sSql;
  dmMfgSchedule.ADOCommand1.Execute;

Open in new window

0
Comment
Question by:Bianca
[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
  • 5
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 36710786
IMHO you should change your query using TOP fDayToAdd to extract just the top desidered records starting from a cartain date

something like




sql.Add('SELECT TOP '+StrToFloat(edMfgDays.Text)+' ID FROM Calendar ');
sql.Add('WHERE (CalendarDays >= '''+DateToStr(dMfgStart)+''') ');
sql.Add('AND OfficeWorkDays <> ''Workday'' ');

Open in new window

0
 

Author Comment

by:Bianca
ID: 36711415
SELECT     TOP (5) CalendarDays
FROM         Calendar
WHERE     (CalendarDays >= '11/15/11') AND (OfficeWorkdays <> 'Workday')

Using my example of Start Date = 11/15/11 and Days in Mfg = 5, the resulting date SHOULD be 11/21/11.

The result of the above query returns the top 5 records that are NOT workdays... I am unsure how to apply it to retrieve the result I am looking for... ??

Thanks!
0
 

Author Comment

by:Bianca
ID: 36711462
I changed the query to:

SELECT     TOP (5) CalendarDays
FROM         Calendar
WHERE     (CalendarDays >= '11/15/11') AND (OfficeWorkdays <> 'Workday')

and the result shows the last record as THE DATE I am looking for... how do I take the LAST record from the resulting dataset?

Instead of TOP(5) is there a way to get LAST(1) of the TOP(5) or something like that?
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Assisted Solution

by:Bianca
Bianca earned 0 total points
ID: 36712057
SELECT     MAX(CalendarDays) AS CalendarDays
FROM         (SELECT     TOP (5) CalendarDays
                       FROM          Calendar
                       WHERE      (CalendarDays >= '11/15/11') AND (OfficeWorkdays = 'Workday')) AS Calendar_1

This gives me the MAX Result from my TOP select! Thanks for your assistance!
0
 

Author Closing Comment

by:Bianca
ID: 36898957
I found the answer during my testing.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 36714935
Back to my office now.

I didn't know the data type so I've suggested to use Top to get the first matching records.
Now I understand exactly what was the need and yes, Top combined with Max do the trick
Glad to have helped you.

Just one thing: you can close the q. by yourself accepting and grading what you prefere instead of asking for a closing. Same result, but a little bit time and job saved by moderators ;-)
0
 

Author Comment

by:Bianca
ID: 36719854
Thanks Ferrucio! Check out my other question if you have time... similar to this one...

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27346575.html
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

752 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