How to check for null values before doing .AddDays() in SSRS?

roman m picture roman m · Sep 26, 2008 · Viewed 15k times · Source

I have the following as the value for my textbox in SSRS report:

   =iif(IsNothing(Fields!MyDate.Value), "", Format(Fields!MyDate.Value.AddDays(30), "MMMM dd, yyyy"))

It gives me an "#Error" every time MyDate is null.

How do i work around this?

UPDATE:

i wrote this custom function, it got rid of the error, but returns January 31, 0001 when null date is passed.

Public Shared Function NewDate(myDate as DateTime, days as integer) AS string
IF ISNOTHING(myDate) OR ISDBNULL(myDate) Then
    NewDate = "        "
ELSE
    NewDate = Format(myDate.AddDays(days), "MMMM dd, yyyy")
END IF
End Function 

@Matt Hamilton: DateAdd("d", 30,Fields!MyDate.Value)

Answer

Matt Hamilton picture Matt Hamilton · Sep 26, 2008

The problem, of course, is that VB's IIF statement evaluates both sides regardless of the outcome. So even if your field is null it's still evaluating the "Value.DateAdd" call.

If I recall correctly, SSRS has its own "DateAdd" function that you can use instead. So you can do something like this (check the documentation 'coz this is from memory):

=Iif(IsNothing(Fields!MyDate.Value), "", Format(DateAdd("d", 30, Fields!MyDate.Value), "MMMM dd, yyyy"))