Solved

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

Posted on 2011-09-27
7
324 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now