Difference between two time values in SSRS report

190 views Asked by At

I have the following select statement:

SELECT [TR_DATE]
      ,[TR_TIME]
      ,RIGHT('000000' + CONVERT(varchar,TR_TIME), 6)
      ,TIMEFROMPARTS(SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),1,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),3,2), SUBSTRING(RIGHT('000000' + CONVERT(varchar,TR_TIME), 6),5,2),0,0) as trtime
      ,[ET_TYPENO]
      ,[ET_NAME]
  FROM [DB400].[dbo].[TRANSACTIONREPORT]
  where DEPT_NAME = 'GIJIMA AST HOLDINGS (PTY) LTD'
  /*and LOC_NAME = 'Front Door and Gate'*/
  and TR_DATE between '20200120' and '20200130'
  order by TR_DATE, MST_LASTNAME, MST_FIRSTNAME, TR_TIME

which returns the following data:

enter image description here

I would like to use this to calculate the time the was spent in the building.

Note that ET_TYPENO = 1 equates to "Allowed Normal In" and ET_TYPENO = 2 to "Allowed Normal Out"

I have the following expression inside an ssrs report

=iif(Fields!ET_TYPENO.Value=2,
DateDiff(DateInterval.Hour, previous(Fields!trtime.Value),Fields!trtime.Value),
"")

But it resolves to the following #Error.

UPDATE

Expected Result

Calculate the time difference between the "Allowed Nornmal In" event and the "Allowed Normal Out" event.

Take line 2 and 3 for example. "Allowed Nornmal In" occured at 07:23:19 and "Allowed Normal Out" occured at 08:55:48. I need it to return the time difference between these two times. I.E. 1:32:29.

2

There are 2 answers

0
Sarah Richardson On BEST ANSWER

Taking the suggestion from Honnover Fist's answer that the parameter of the DateDiff need to be Dates instead of Times. (The Documentation does say that a Time will work). I have changed my query to a DATETIMEFROMPARTS() instead of a TIMEFROMPARTS().

Then I changed the condition to the below code so that the hours would be displayed more accurately.

=IIF(Fields!ET_TYPENO.Value = 2, 
    DateDiff(DateInterval.Minute, previous(Fields!TR_DATETIME.Value), Fields!TR_DATETIME.Value) / 60,
    NOTHING)
2
Hannover Fist On

The trtime needs to be converted to a DATE type with the CDATE() function to use the DATEDIFF function.

=IIF(Fields!ET_TYPENO.Value = "OUT", 
    DateDiff(DateInterval.Hour, CDATE(previous(Fields!trtime.Value)), CDATE(Fields!trtime.Value)),
    NOTHING)

Also, shouldn't Fields!ET_TYPENO.Value = 1 ?