I'm using CR 2008 with an ODBC connection to a db on SQL Server 2005. I need to calculate the difference between 2 dates, excluding non-business hours. I've almost got it working. The following code reassigns the date values correctly and calculates the difference between them correctly. BUT, while it shows the adjusted start and end dates on the right line, it shows the difference on the following line with the next set of dates. The first record shows zeroes for the difference in the dates. How can I fix this?
Thanks in advance for any help!
Code:
whileprintingrecords;
datetimevar createdate:={Cases.Case_CreatedDate};
numbervar createyear:=datepart("yyyy", {Cases.Case_CreatedDate});
numbervar createmonth:=datepart("m", {Cases.Case_CreatedDate});
numbervar createday:=datepart("d", {Cases.Case_CreatedDate});
//numbervar createhr:=datepart("h", {Cases.Case_CreatedDate});
//numbervar createmin:=datepart("n", {Cases.Case_CreatedDate});
//numbervar createsec:=datepart("s", {Cases.Case_CreatedDate});
numbervar createdayofweek:=dayofweek({Cases.Case_CreatedDate});
datetimevar respondate:={CaseProgress.Case_TimeStamp};
numbervar respondyear:=datepart("yyyy", {CaseProgress.Case_TimeStamp});
numbervar respondmonth:=datepart("m", {CaseProgress.Case_TimeStamp});
numbervar responday:=datepart("d", {CaseProgress.Case_TimeStamp});
//numbervar respondhr:=datepart("h", {CaseProgress.Case_TimeStamp});
//numbervar respondmin:=datepart("n", {CaseProgress.Case_TimeStamp});
//numbervar respondsec:=datepart("s", {CaseProgress.Case_TimeStamp});
numbervar respondayofweek:=dayofweek({CaseProgress.Case_TimeStamp});
numbervar secadjust:=0; //if interval spans a weekend(s) or evenings then time is removed
numbervar daydiff:=0; //number of days between create and respond
numbervar respondtime;
numbervar Days := Truncate (respondtime / 86400);
numbervar Hours := Truncate (Remainder ( respondtime , 86400) / 3600) ;
numbervar Minutes := Truncate (Remainder ( respondtime , 3600) / 60) ;
numbervar Seconds := Remainder (respondtime , 60) ;
shared numbervar respondflag; //reset at the case group header level
If {CaseProgress.Case_Stage}="Responded" then //and respondflag=0 then
(
if createdayofweek = 7 then //if Saturday, make Monday 8am
createdate := cdatetime(createyear,createmonth,createday+2,8,00,00)
else if createdayofweek = 1 then //if Sunday, make Monday 8am
createdate := cdatetime(createyear,createmonth,createday+1,8,00,00)
else if createdayofweek = 6 and time(createdate) > time(16,59,59) then //if Friday after 5pm, make Monday 8am
createdate := cdatetime(createyear,createmonth,createday+3,8,00,00)
else if time(createdate) > time(16,59,59) then //if after 5pm, make next day 8am
createdate := cdatetime(createyear,createmonth,createday+1,8,00,00)
else if time(createdate) < time(8,00,00) then //if before 8am, make 8am
createdate := cdatetime(createyear,createmonth,createday,8,00,00);
if respondayofweek = 7 then //if Saturday, make Monday 8:00:01am
respondate := cdatetime(respondyear,respondmonth,responday+2,8,00,01)
else if respondayofweek = 1 then //if Sunday, make Monday 8:00:01am
respondate := cdatetime(respondyear,respondmonth,responday+1,8,00,01)
else if respondayofweek = 6 and time(respondate) > time(16,59,59) then //if Friday after 5pm, make Monday 8:00:01am
respondate := cdatetime(respondyear,respondmonth,responday+3,8,00,01)
else if time(respondate) > time(16,59,59) then //if after 5pm, make next day 8:00:01am
respondate := cdatetime(respondyear,respondmonth,responday+1,8,00,01)
else if time(respondate) < time(8,00,00) then //if before 8am, make 8:00:01am
respondate := cdatetime(respondyear,respondmonth,responday,8,00,01);
daydiff := DateDiff("d", createdate, respondate);
if daydiff >= 1 then //remove the number of seconds of nonbusiness hour time
(if DateDiff("ww",createdate, respondate) <> 0 then //remove the number of seconds used by the 63 hrs of the weekend
(daydiff := daydiff - 3;
secadjust := 226800 * DateDiff("ww",createdate, respondate));
secadjust := secadjust + (54000 * daydiff));
respondtime:=DateDiff("s", createdate, respondate)-secadjust;
respondflag:=1;
totext(createdate, "MM/dd/yyyy hh:mm:sstt") + ' ' +
totext(respondate, "MM/dd/yyyy hh:mm:sstt") + ' ' +
Totext ( Days , '##' ) + ':' +
Totext ( Hours , '00' ) + ':' +
Totext ( Minutes , '00' ) + ':' +
Totext ( Seconds , '00' )
)
Thanks in advance for any help!