
# instr1
instr1函数用于计算子串str2在字符串str1中的位置。
相似函数：[instr](https://support.huaweicloud.com/sqlref-spark-dli/dli_spark_instr.html)，instr函数用于返回substr在str中最早出现的下标。但是instr不支持指定起始搜索位置和匹配次数。
#### 命令格式
```
instr1(string <str1>, string <str2>[, bigint <start_position>[, bigint <nth_appearance>]])
```
#### 参数说明
表1参数说明 
| 参数             | 是否必选 | 参数类型   | 说明                                                                                                                                               |
|:---|:---|:---|:---|
| str1           | 是    | STRING | 待搜索的目标字符串。 如果输入为BIGINT、DOUBLE、DECIMAL或DATETIME类型，则会隐式转换为STRING类型后参与运算，其他类型会返回报错。 |
| str2           | 是    | STRING | 待匹配的子串。 如果输入为BIGINT、DOUBLE、DECIMAL或DATETIME类型，则会隐式转换为STRING类型后参与运算，其他类型会返回报错。     |
| start_position | 否    | BIGINT | 表示从str1的第几个字符开始搜索，默认起始位置是第一个字符位置1。 不支持指定负数。                                       |
| nth_appearance | 否    | BIGINT | 表示str2在str1中第nth_appearance次匹配的位置。 如果nth_appearance为其他类型或小于等于0，则返回报错。            |
   
#### 返回值说明
返回BIGINT类型。
![](https://support.huaweicloud.com/sqlref-spark-dli/public_sys-resources/note_3.0-zh-cn.png)
- 如果在str1中未找到str2，则返回0。
- 如果str2为空串，则总能匹配成功。
- str1、str2、start_position或nth_appearance值为NULL时，返回NULL。
 
#### 示例代码
返回 10
```
select instr1('Tech on the net', 'h', 5, 1);
```
返回2。
```
select instr1('abc', 'b');
```
返回NULL。
```
select instr('abc', null);
```
