简体   繁体   中英

Prolem with IIF statement in SSRS

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:

  • Wrapping the function in an ISERROR is creating an error on the bad rows, rather than returning true .
  • Using an 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.

The issue is the -1 at the end of the left statement: in instances without a hyphen -1 would be -1 on 0, causing an error.

To fix this I have used this expression,

=StrConv(
    Trim(
      Replace(
            IIF(
               INstr(Fields!Introducer_Title.Value, "-") <> 0, 
               Left(Fields!Introducer_Title.Value, INstr(Fields!Introducer_Title.Value, "-")),
               Fields!Introducer_Title.Value
               ),
            "-",
            nothing
            )
        ),
      VbStrConv.ProperCase
      )

Note that while this solves my problem, at the heart of the problem is the fact that SSRS is trying to execute all functions before checking which one to return, and so erroring when it hits one it cannot execute. This is why I have marked the other answer as correct, because that was the true cause of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM