Jonathan Porter
Jonathan Porter

Reputation: 1556

SSRS - Expression returning error if null

I'm expecting only the names to appear on the calendar if they exist and nothing if they don't.

I created an expression where it returns only the last name of a person by using the comma as a delimiter.

My current expression:

=iif(IsNothing(Fields!EmployeeName.Value), nothing, Left(Fields!EmployeeName.Value,-1 + InStr(Fields!EmployeeName.Value, ",")))

Current results where #Error appears if a name doesn't exist:

enter image description here

Upvotes: 0

Views: 1026

Answers (1)

Chris Albert
Chris Albert

Reputation: 2507

The error is occuring because you are passing in a number less than 0 to the left function. When your string does not have a comma in it you are passing in -1.

To handle this I added two if statements to the expression. The first will return the whole string if the index of the first comma is 0. The second checks for the -1 condition and passes a 0 to the left function when that occurs.

=
iif(
    IsNothing(Fields!EmployeeName.Value), 
    nothing, 
    iif(
        InStr(Fields!EmployeeName.Value, ",") = 0, 
        Fields!EmployeeName.Value, 
        Left(Fields!EmployeeName.Value,iif(-1 + InStr(Fields!EmployeeName.Value, ",") < 0, 0, -1 + InStr(Fields!EmployeeName.Value, ",")))
        )
    )

Upvotes: 2

Related Questions