Display blank value (“”) as date in SSRS

巧了我就是萌 提交于 2019-12-08 02:43:15

问题


I have the column DiscontinuedDate with either a datetime or a blank value. I used the expression

FormatDateTime(Fields!DiscontinuedDate.Value, DateFormat.ShortDate)

To show the date time as just a date but then when the value is blank it shows as an error with the following message "Conversion from string "" to type 'Date' is not valid."

So i've been trying to use an IIF expression like the below:

=IIF(Fields!DiscontinuedDate.Value is "", "", FormatDateTime(Fields!DiscontinuedDate.Value, DateFormat.ShortDate))

I've tried a few variations but they all bring back the same error. Any ideas?

Thanks,

Adam


回答1:


Your issue is that SSRS IIf expressions do not short circuit, so whenever you have a blank string your code will still be trying the FormatDateTime conversion, so you get this error even with your check.

You can add some logic to stop the empty string being evaluated in the FormatDateTime expression by using another IIf to change it to a NULL value, which won't fail:

=IIF(Fields!DiscontinuedDate.Value = ""
    , ""
    , FormatDateTime(IIf(Fields!DiscontinuedDate.Value = ""
            , Nothing
            , Fields!DiscontinuedDate.Value)
        , DateFormat.ShortDate))

That solves your immediate issue, but assuming the underlying data is text based, I would also recommend looking at your underlying data and using explicit DateTime type data types instead of strings to prevent these at the lowest possible level.



来源:https://stackoverflow.com/questions/18915970/display-blank-value-as-date-in-ssrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!