语法格式 INSTR(str,substr) INSTR(str,substr,start_position,N) 表5- 10 参数说明 参数 说明 str 字符串母串。 substr 字符串子串。 start_position 表示从字符串(左端)第几个字符开始匹配, 如是负数,则 从右端往前反向查找子串。可选参数,默认为1。 nth_appearance 从start_position 开始向字符串尾方向查找第几个匹配字符 串。可选参数,默认为1。 函数说明 第一种语法功能: 返回子串substr 在字符串str(左端开始)第一次出现的位置。如果没有,返 回0。如果有一个参数为NULL,则返回NULL。 第二种语法功能: 1. 查找第nth_appearance 个匹配字符串的功能; 2. 从母串的第start_position 个字符开始查找匹配字符串的功能; 3. 支持第三个参数start_position 为负数的情况,即从母串右侧第 |start_position|个位置反向查找子串的功能。 GBase 8a MPP Cluster 产品手册 5 数据库管理指南 文档版本953(2022-04-10) 南大通用数据技术股份有限公司 691 说明 substr 在str 中的位置,以1 开始计数。 返回的位置是按全字符串正向位置计数的,与从哪个位置开始无关 示例 示例1:返回“bar”在“foobarbar”中第一次出现的位置。 gbase> SELECT INSTR('foobarbar', 'bar') FROM dual; +---------------------------+ | INSTR('foobarbar', 'bar') | +---------------------------+ | 4 | +---------------------------+ 1 row in set 示例2:“foobar”不在“xbar”中。 gbase> SELECT INSTR('xbar', 'foobar') FROM dual; +-------------------------+ | INSTR('xbar', 'foobar') | +-------------------------+ | 0 | +-------------------------+ 1 row in set 示例3:如有任一参数是一个二进制字符串,则它是字母大小写敏感的。 gbase> SELECT INSTR('foobarbar', BINARY 'Bar') FROM dual; +----------------------------------+ | INSTR('foobarbar', BINARY 'Bar') | +----------------------------------+ | 0 | +----------------------------------+ 1 row in set 示例4:从字符串“beiwelcometobeijing”第三个字符开始匹配,查找“bei”第一 次出现的位置。 gbase> SELECT INSTR ('beiwelcometobeijing','bei',3) FROM dual; +-----------------------------------+ | INSTR('beiwelcometobeijing','bei',3) | +-----------------------------------+ | 13 | +-----------------------------------+ 1 row in set GBase 8a MPP Cluster 产品手册 5 数据库管理指南 文档版本953(2022-04-10) 南大通用数据技术股份有限公司 692 示例5: 从字符串 “112111222222333333” 第一个字符开始匹配, 查找第二个 “11” 出现的位置。 gbase> SELECT INSTR ('112111222222333333','11',1,2) FROM dual; +--------------------------------------+ | INSTR('112111222222333333','11',1,2) | +--------------------------------------+ | 4 | +--------------------------------------+ 1 row in set 示例6:从字符串“welcometochina”倒数第一个字符开始匹配,查找第一个“e” 出现的位置。 gbase> SELECT INSTR ('welcometochina','e',-1,1) FROM dual; +----------------------------------+ | INSTR('welcometochina','e',-1,1) | +----------------------------------+ | 7 | +----------------------------------+ 1 row in set