Instr (MDX)

傳回第一個出現在另一個字串內的位置。

語法

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

引數

開始
(選擇性)數值運算式,設定每個搜尋的起始位置。 如果省略此值,搜尋會從第一個字元位置開始。 如果 start 為 null,則函式傳回值未定義。

searched_string
要搜尋的字串運算式。

search_string
要搜尋的字串運算式。

比較
(選擇性)整數值。 這個引數一律會被忽略。 其定義是為了與 其他語言中的其他 Instr 函式相容。

傳回值

具有 String1 String2 起始位置的 整數值。

此外, InStr 函式會根據條件傳回下表所列的值:

條件 傳回值
String1 長度為零 零 (0)
String1 為 null 未定義
String2 長度為零 start
String2 為 null 未定義
找不到 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]  

下表顯示取得的結果。

量值中的欄位 結果
小寫字串中找到小寫 16
小寫字串中找到的大寫 16
搜尋的字串是空的 0
搜尋的字串為 null 未定義
搜尋字串是空的 1
搜尋字串是空的開頭 10 10
搜尋字串為 null 未定義
從 10 開始找到 16
從 17 開始找不到 0
Null 啟動 未定義
開始大於搜尋長度 0