Instr (MDX)

 

Updated: March 2, 2016

Returns the position of the first occurrence of one string within another.

InStr([start, ]searched_string, search_string[, compare])  

start
(Optional) A numeric expression that sets the starting position for each search. If this value is omitted, the search begins at the first character position. If start is null, the function return value is undefined.

searched_string
The string expression to be searched.

search_string
The string expression that is to be searched for.

Compare
(optional) An integer value. This argument is always ignored. It is defined for compatibility with other Instr functions in other languages.

An integer value with the starting position of String2 in String1.

Also, InStr function returns the values listed in the following table depending on the condition:

ConditionReturn value
String1 is zero-lengthzero (0)
String1 is nullundefined
String2 is zero-lengthstart
String2 is nullundefined
String2 is not foundzero (0)
start is greater than Len(String2)zero (0)
System_CAPS_ICON_warning.jpg Warning


Instr always performs a case-insensitive comparison.

The following example shows the usage of the Instr function and shows different result scenarios.

with   
    member [Date].[Date].[Results] as "Results"  
    member measures.[lowercase found in lowercase string] as InStr( "abcdefghijklmnñopqrstuvwxyz", "o")  
    member measures.[uppercase found in lowercase string] as InStr( "abcdefghijklmnñopqrstuvwxyz", "O")  
    member measures.[searched string is empty]            as InStr( "", "o")  
    member measures.[searched string is null]             as iif(IsError(InStr( null, "o")), "Is Error", iif(IsNull(InStr( null, "o")), "Is Null","Is undefined"))  
    member measures.[search string is empty]              as InStr( "abcdefghijklmnñopqrstuvwxyz", "")  
    member measures.[search string is empty start 10]     as InStr(10, "abcdefghijklmnñopqrstuvwxyz", "")  
    member measures.[search string is null]               as iif(IsError(InStr( null, "o")), "Is Error", iif(IsNull(InStr( null, "o")), "Is Null","Is undefined"))  
    member measures.[found from start 10]                 as InStr( 10, "abcdefghijklmnñopqrstuvwxyz", "o")  
    member measures.[NOT found from start 17]             as InStr( 17, "abcdefghijklmnñopqrstuvwxyz", "o")  
    member measures.[NULL start]                          as iif(IsError(InStr( null, "abcdefghijklmnñopqrstuvwxyz", "o")), "Is Error", iif(IsNull(InStr( null, "abcdefghijklmnñopqrstuvwxyz", "o")), "Is Null","Is undefined"))  
    member measures.[start greater than searched length]  as InStr( 170, "abcdefghijklmnñopqrstuvwxyz", "o")  
  
select  [Results] on columns,  
       { measures.[lowercase found in lowercase string]  
       , measures.[uppercase found in lowercase string]  
       , measures.[searched string is empty]  
       , measures.[searched string is null]  
       , measures.[search string is empty]  
       , measures.[search string is empty start 10]  
       , measures.[search string is null]  
       , measures.[found from start 10]  
       , measures.[NOT found from start 17]  
       , measures.[NULL start]   
       , measures.[start greater than searched length]  
       } on rows  
  
from [Adventure Works]  

The following table displays the obtained results.

Results
lowercase found in lowercase string16
uppercase found in lowercase string16
searched string is empty0
searched string is nullIs undefined
search string is empty1
search string is empty start 1010
search string is nullIs undefined
found from start 1016
NOT found from start 170
NULL startIs undefined
start greater than searched length0

Community Additions

ADD
Show: