Instr (MDX)

返回一个字符串在另一字符串中第一次出现的位置。

语法

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

参数

start
(可选)设置每个搜索的起始位置的一个数值表达式。 如果省略此值,则搜索将会在第一个字符位置开始。 如果 start 为 null,则函数返回值未定义。

searched_string
要搜索的字符串表达式。

search_string
要对其进行搜索的字符串表达式。

比较
(可选)一个整数值。 始终忽略此参数。 它的定义是为了与其他语言中的其他 Instr 函数兼容。

返回值

一个整数值,字符串 1 中的起始位置为 String2

此外, InStr 函数根据条件返回下表中列出的值:

条件 返回值
String1 为零长度 零 (0)
String1 为 Null undefined
String2 为零长度 start
String2 为 Null undefined
找不到 String2 零 (0)
start 大于 Len(String2) 零 (0)

备注

警告

Instr 始终执行不区分大小写的比较。

示例

以下示例演示 Instr 函数的用法,并显示了不同的结果方案。

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]  

下表显示了获得的结果。

度量值中的字段 结果
lowercase found in lowercase string 16
uppercase found in lowercase string 16
searched string is empty 0
searched string is null 未定义
search string is empty 1
search string is empty start 10 10
search string is null 未定义
found from start 10 16
NOT found from start 17 0
NULL start 未定义
start greater than searched length 0