Display blank value (“”) as date in SSRS

datenullreporting-services

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

Best Answer

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.