问题描述
我有一个DiscontinuedDate列与datetime或空值。我使用表达式 FormatDateTime(Fields!DiscontinuedDate.Value,DateFormat.ShortDate)
要将日期时间显示为日期,但是当该值为空时,显示为错误,并显示以下消息从字符串转换键入日期无效。
所以我一直在尝试使用如下所示的IIF表达式:
= IIF(Fields!DiscontinuedDate.Value is,,FormatDateTime(Fields!DiscontinuedDate.Value,DateFormat.ShortDate))
我已经尝试了一些变体,但都会带回相同的错误。任何想法?
谢谢,
Adam
您的问题是SSRS IIf
表达式不会短路,所以每当你有一个空白字符串,你的代码仍然会尝试 FormatDateTime
转换,所以即使你的支票也会收到这个错误。
你可以添加一些逻辑来停止空在 FormatDateTime
表达式中使用另一个 IIf
将其更改为 NULL 值,不会失败:
= IIF(Fields!DiscontinuedDate.Value =
,
,FormatDateTime(IIf(Fields!DiscontinuedDate.Value =
,Nothing
,Fields!DiscontinuedDate.Value)
,DateFormat.ShortDate))
解决了您的直接问题,但假设底层数据是基于文本的,我还建议您查看基础数据,唱出明确的DateTime类型的数据类型而不是字符串,以防止这些在最低的可能级别。
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
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.
这篇关于在SSRS中显示空白值(“”)作为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!