The Setup
I have an expression that converts all caps values to proper case. Some of the values have an extra bit preceded by a hyphen, I want everything before that. Sadly a LEFT(INStr
expression errors on instances without a hyphen in.
=IIF(
INstr(Fields!Introducer_Title.Value, "-") = 0,
StrConv(Fields!Introducer_Title.Value,vbStrConv.ProperCase),
"nope"
)
The above works just fine: if there is no hyphen then the string is converted to proper case, otherwise it just says nope.
The Problem
=IIF(
INstr(Fields!Introducer_Title.Value, "-") = 0,
StrConv(Fields!Introducer_Title.Value,vbStrConv.ProperCase),
Left(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase), INstr(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase), "-")-1)
)
The above works in so far as that if there is a hyphen in the value it returns the text before it in proper case, but now the values without hyphens error. The logic hasn’t changed.
It's as if instead of going IIF(A=TRUE,This,That) is somehow converted to always do That when I replace the Otherwise "nope" with a nested expression.
The error is just #Error, no other information.
Am I missing something obvious? I have a feeling this is some quirk of SSRS.
Updates
This is getting stranger the more I look into it:
ISERROR
is creating an error on the bad rows, rather than returning true
.InStrRev
function is returning the same value as an InStr
function even though the position of the hyphen in the first row is not in the middle (I checked the values in Excel)IF(A=TRUE, This, That)
. SSRS will always evalute this and that irrespective of value of A.
In other terms IIF
conditions in SSRS are not short circuited.
Try something like this.
Method 1:
=Left(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase),
IIF(
INstr(Fields!Introducer_Title.Value, "-") = 0,
LEN(Fields!Introducer_Title.Value),
INstr(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase), "-")-1
)
)
Method 2:
=IIF(
INstr(Fields!Introducer_Title.Value, "-") = 0,
StrConv(Fields!Introducer_Title.Value,vbStrConv.ProperCase),
Left(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase),
IIF(
INstr(Fields!Introducer_Title.Value, "-") = 0,
0,
INstr(StrConv(Fields!Introducer_Title.Value, VbStrConv.ProperCase), "-")-1
)
)
)
Note: The above code is not tested. Check for brackets and length of strings to make sure you get the correct result.