cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate the time duration on a datetime column?

0 Kudos

Hi guys,

I've done some search on this forum and everywhere else but I can't seem to get this right, at the beggining it sounded like something very simple to accomplish, for the instance with Excel but I'm struggling to get it to work with Crystal Reports on Microsoft Visual Studio 2008.

I have a datetime column (SQL Server 2000) that I wanted to calculate the the time duration on the report group footer, unfortunatelly the built-in SUM function cannot be applied and I've tried several formulas that I've found on the internet without any luck. I'm using a datetime column to store only the time because I'm stuck with SQL Server 2000 which doesn't have a time data type.

Would you guys know how to do it by any chance?

Some sample code I've tried: http://www.minisolve.com/TipsAddUpTime.htm

Thanks a lot,

Paul

Edited by: Paul Doe on Dec 12, 2009 5:41 PM

Some sample data:

EMPLOYEE WORK HOURS

========= =================

JOHN DOE 1900-01-01 01:00:05

JOHN DOE 1900-01-01 00:20:00

JOHN DOE 1900-01-01 01:30:15

========= =================

HOURS WORKED: 02:50:20

Edited by: Paul Doe on Dec 12, 2009 5:42 PM

Edited by: Paul Doe on Dec 12, 2009 5:45 PM

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Guess what, by further testing the code on the website mentioned above I got it working.

Pus, I needed to change the grouping on the code, so I had to come up with a way to update the formulas based on the groupping field.

Considering "call_date" is the field that you are groupping by on the designer use the following code to update the formula:


CrystalReportObj = new ReportDocument();
CrystalReportObj.Load("C:\\reportfile.rpt");

//
FieldDefinition FieldDef;

//Get formula
FormulaFieldDefinition FormulaDef1;
FormulaDef1 = CrystalReportObj.DataDefinition.FormulaFields["SubHours"];

//Get formula
FormulaFieldDefinition FormulaDef2;
FormulaDef2 = CrystalReportObj.DataDefinition.FormulaFields["subMinSec"];

//Update the formula to work with the new grouping field, 
//this must be called first else will throw an exception
FormulaDef1.Text = FormulaDef1.Text.Replace("call_date", "call_extension");
FormulaDef2.Text = FormulaDef2.Text.Replace("call_date", "call_extension");

//Get the new field we are grouping by
FieldDef = CrystalReportObj.Database.Tables[0].Fields["call_extension"];

//Replace current grouping field with the new one, 
//considering there only one group in the report, index 0
CrystalReportObj.DataDefinition.Groups[0].ConditionField = FieldDef;

Have fun.

Edited by: Paul Doe on Dec 12, 2009 8:43 PM

Edited by: Paul Doe on Dec 12, 2009 8:53 PM

Answers (0)