Search code examples
reporting-servicesssrs-2008-r2ssrs-2012ssrs-tablix

why do we give >0 in instr function ssrs. The InStr function works without giving the greater than 0 value


why do we give >0 in instr function ssrs. The InStr function works without giving the greater than 0 value.


Solution

  • Because InStr returns index of first appearance of search string in string that is searched. Index is 1-based, and when string not found, the returned index is lowerbound(string as array of chars) - 1 = 1 - 1 = 0. This is in VB, in C# f.e. it will be 0 - 1 = -1.

    When you're using InStr to determine if string is found, you need boolean result, and to get it you use comparison > 0, which returns True when string found and False otherwise.

    But, since there is implicit conversion exist between boolean and int, you can use InStr directly, and return value 0 (not found) will be converted to False, while any non-zero value (found) will be converted to True.

    Although this is correct and works, this way is less obvious, and taking a look at the code =InStr(...) you can't quickly say, real index needed (integer) or the fact that this index exist (boolean). Finally it's all about code readability and maintainability, don't forget that implicit conversions (especially in not-debuggable SSRS code) may bring you a lot of problems.