Tuesday, November 23, 2010

Coding for Null dates resulting in a Blank cell

I had a need within a SQL Server Reporting Services (SSRS) to handle Null dates and it was not as straight forward as I expected. 

Here is the situation.  The column is a data time data type coming from SQL Server.  The date needed to be formatted in MM/DD/YYYY and I created a Format Function in the expression for the column on the report.  It looked like this

FormatDateTime(Fields!Min_Date.Value,vbShortDate)

The Null dates resulted in this output - 1/1/0001.  I wanted the column to be blank for the Null dates.

Next I coded a statement that looked like this

Iif(IsNothing(Fields!Min_Date), "",FormatDateTime(Fields!Min_Date.Value,vbShortDate))

This also resulted in the date looking like 1/1/0001 and not the desired blank.  Although I thought it would since the IsNothing function should have caught the Null date and produced a blank in the true condition.

My next attempt was to code a double Iif like this

=Iif(Iif(IsNothing(Fields!Min_Date), "",FormatDateTime(Fields!Min_Date.Value,vbShortDate)) = "1/1/0001","",FormatDateTime(Fields!Min_Date.Value,vbShortDate))

This provided the blank cell.  One of my colleagues pointed out that the IsNothing function was not necessary and it was removed.  The final code looks like this and produces the desired result.

=Iif(FormatDateTime(Fields!Min_Date.Value,vbShortDate) = "1/1/0001","",FormatDateTime(Fields!Min_Date.Value,vbShortDate))