Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 734
roman m Avatar asked Sep 25 '08 23:09

roman m


1 Answers

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"))
like image 187
Matt Hamilton Avatar answered Sep 29 '22 13:09

Matt Hamilton