There's a requirement to display a date range based on the following logic in a Reporting Services report like so:
If there's only a Started datetime, display like so:
on 12 December 2014
If there's a Started and Finished datetime on the same day, display like so:
on 12 December 2014 between 11:20 am and 1:10 pm
If there's a Started and Finished datetime on different days, display like so:
between 12 December 2014 11:20 am and 13 December 2014 1:10 pm
I know it's ugly, but I've got this expression to display the field:
=IIf(IsNothing(First(Fields!Finished.Value, "InspectionAdvice")), "on " & Day(First(Fields!Started.Value, "InspectionAdvice")) & " " & MonthName(Month(First(Fields!Started.Value, "InspectionAdvice"))) & " " & Year(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(
Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Started.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Started.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12, "pm", "am")
,
IIf(First(Fields!Started.Value.Date, "InspectionAdvice") = First(Fields!Finished.Value.Date, "InspectionAdvice")
, "on " & Day(First(Fields!Started.Value, "InspectionAdvice")) & " " & MonthName(Month(First(Fields!Started.Value, "InspectionAdvice"))) & " " & Year(First(Fields!Started.Value, "InspectionAdvice")) & " between " &
IIf(
Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Started.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Started.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12, "pm", "am")
& " and " &
IIf(
Hour(First(Fields!Finished.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Finished.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Finished.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Finished.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Finished.Value, "InspectionAdvice")) > 12, "pm", "am")
, "between " & Day(First(Fields!Started.Value, "InspectionAdvice")) & " " & MonthName(Month(First(Fields!Started.Value, "InspectionAdvice"))) & " " & Year(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(
Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Started.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Started.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12, "pm", "am")
& " and " & Day(First(Fields!Finished.Value, "InspectionAdvice")) & " " & MonthName(Month(First(Fields!Finished.Value, "InspectionAdvice"))) & " " & Year(First(Fields!Finished.Value, "InspectionAdvice")) & " " &
IIf(
Hour(First(Fields!Finished.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Finished.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Finished.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Finished.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Finished.Value, "InspectionAdvice")) > 12, "pm", "am")
)
)
Everything works fine if both Started and Finished are not null. However, if Finished is null then I always get #Error.
Now if I remove the second part of the IIF containing the nested IIf logic e.g.
=IIf(IsNothing(First(Fields!Finished.Value, "InspectionAdvice")), "on " & Day(First(Fields!Started.Value, "InspectionAdvice")) & " " & MonthName(Month(First(Fields!Started.Value, "InspectionAdvice"))) & " " & Year(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(
Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12,
Hour(First(Fields!Started.Value, "InspectionAdvice")) - 12,
Hour(First(Fields!Started.Value, "InspectionAdvice"))
)
& ":" & Minute(First(Fields!Started.Value, "InspectionAdvice")) & " " &
IIf(Hour(First(Fields!Started.Value, "InspectionAdvice")) > 12, "pm", "am")
,
"REMOVED"
)
Things work when Finished is null.
Any idea why I can't get both parts of the ugly statement working together? I'm guessing Reporting Services is trying to resolve some of the IIF's in the false condition which will hit the NULL Finished field and so result in errors?