SSRS - 复杂的日期范围字段逻辑不能与嵌套的IIF

时间:2015-07-20 22:15:13

标签: reporting-services vbscript ssrs-2012 ssrs-expression

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

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

  

于2014年12月12日

如果同一天有一个已启动和已完成的日期时间,则显示如下:

  

于2014年12月12日上午11:20至下午1:10

如果在不同日期有一个已开始和已完成的日期时间,则显示如下:

  

2014年12月12日上午11:20至2014年12月13日下午1:10

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

=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都不为null,则一切正常。但是,如果Finished为null,那么我总是得到#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正在尝试解决一些处于NULL Finished字段的错误状态的IIF,从而导致错误?

2 个答案:

答案 0 :(得分:2)

不确定原因,但比较日期的部分导致错误:

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

结果: result in report builder 3.0

答案 1 :(得分:2)

在SSRS中,对两个IIF分支进行评估。见here。因此错误,正如你正确怀疑的那样。

尝试修改数据集InspectionAdvice,以便在没有结束日期时返回空白而不是null。您可以使用SQL ISNULL函数

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