SSRS - 复杂的日期范围字段逻辑不适用于嵌套的 IIF

SSRS - Complex date range field logic not working with nested IIF's

需要在 Reporting Services 报告中根据以下逻辑显示日期范围,如下所示:

如果只有开始日期时间,则显示如下:

on 12 December 2014

如果开始日期和完成日期时间在同一天,则显示如下:

on 12 December 2014 between 11:20 am and 1:10 pm

如果 Started 和 Finished 日期时间在不同的日期,则显示如下:

between 12 December 2014 11:20 am and 13 December 2014 1:10 pm

我知道这很丑,但我有这个表达式来显示字段:

=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")
)
)

如果 Started 和 Finished 都不为空,一切正常。但是,如果 Finished 为空,那么我总是得到 #Error.

现在,如果我删除包含嵌套 IIf 逻辑的 IIF 的第二部分,例如

=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"
)

当 Finished 为空时一切正常。

知道为什么我不能让丑陋的声明的两个部分一起工作吗?我猜 Reporting Services 正在尝试解决某些 IIF 的错误条件,这将达到 NULL Finished 字段并因此导致错误?

不知道为什么,但是比较日期的部分导致了错误:

IIf(First(Fields!Started.Value.Date, "InspectionAdvice") = First(Fields!Finished.Value.Date, "InspectionAdvice") 

正如您所说,出于某种原因,RS 正在尝试评估 Fields!Finished.Value.Date,这会导致错误。

我简化了你的代码,发现这个解决方法工作正常:

=IIf(IsNothing(Fields!Finished.Value), 
    "finished is null", 
    IIf(DateDiff(DateInterval.Day, Fields!Finished.Value, Fields!Started.Value)=0,  
        "same day",
        "different day"
        )
    )

结果:

在 SSRS 中,IIF 的两个分支都会被评估。参见 here。因此错误,正如你正确地怀疑的那样。

尝试将您的数据集 InspectionAdvice 修改为 return 空白而不是 null,因为它们没有结束日期。您可以使用 SQL ISNULL 函数

SELECT Started, ISNULL(Finished, '') Finished
FROM [SomeTable]