# **MID**

从字符串中指定的起始位置起返回指定长度的字符串，用法与[SUBSTR函数](./SUBSTR.md)相同。

## 语法{#grammar}

MID(**str**, **start\_index**, \[**num**])

- **str**：给定的字符串对象
- **start\_index**：非负整数，规定要提取的子串的第一个字符在str中的位置。合法取值范围是`1`到`len(str)`
  - start\_index>0：在字符串的指定位置开始
  - start\_index<0：从字符串结尾的指定位置开始
  - start\_index=0：返回空字符串
  - start\_index>len(str)：返回空字符串
  - start\_index+num>len(str)：返回的子串一直到字符串的结尾
- **num**：可选的非负整数，返回子串的长度
  - 合法取值范围：`1`到`len(str)-start_index+1`
  - 不传递参数时：返回的子串会一直到字符串的结尾
  - num<1：返回空字符串

**示例地址：** [MID](https://demo.succbi.com/v5/bi/mid)

## 示例{#example}

1. `MID("qwer",2)` 从第二个字符一直到字符串尾，返回`wer`
2. `MID("210502198412020944",7,8)` 返回身份证中的出生日期，返回`19841202`
3. `MID("03-武汉店",1,FIND("03-武汉店",'-')-1)` 返回门店编码，返回`03` [FIND](./FIND.md)函数
4. `MID(A1,1,4)` 静态值返回生产季节的年份，返回`2017`
5. `MID([门店销售明细表].[生产季节],1,4)` 参数值返回生产季节的年份，返回`2017`
6. `MID("asd",0,2)` start\_index=0，返回结果为空字符串
7. `MID("asd",4,2)` start\_index超过字符串长度，返回结果为空字符串
8. `MID("abcdefg",-1,4)` start\_index<0，返回结果从字符串结尾的指定位置开始，返回`g`
9. `MID("asd",1,4)` num超过字符串长度，返回字串一直到字符串的结尾，返回`asd`
10. `MID("asd",2,0)` num=0，返回结果为空字符串
11. `MID("asd",2,-1)` num<1，返回结果为空字符串
12. `MID("湖北省武汉市",5,3)` start\_index+num>len(str)，返回子字符串一直到结尾，返回`汉市`
