Oracle兼容-函数-INSTR()函数


1. 语法

1. INSTR( string1, string2 [, start_position [, nth_appearance ] ] )

2. 定义和用法

INSTR() 函数的作用是返回要截取的字符串在源字符串中的位置。即在 string1 中查找 string2,是从 start_position 给出的偏移量开始在 string1 里查找,查找出第 nth_appearance 次出现 string2 的位置。

3. Oracle兼容说明

在GreatSQL中 INSTR() 函数的使用方法和Oracle相同。

在GreatSQL中已有 INSTR() 函数,其原生用法为:

1. INSTR(str,substr)

在GreatSQL中,扩展为下面的用法:

1. INSTR( string1, string2 [, start_position [, nth_appearance ] ] )

即:INSTR(源字符串, 目标字符串, 起始位置, 匹配序号)。

注意:由于在GreatSQL中已有原生 INSTR() 函数,如果想使用扩展后的 INSTR() 函数,需要先执行 SET sql_mode = ORACLE 切换到 ORACLE 模式。

GreatSQL中 INSTR() 函数与Oracle不同之处有:当参数 nth_appearance 值为小数时,结果与Oracle不一致(Oracle会做特殊转换处理),详见下方示例。

4. 示例

下面几个案例展示在GreatSQL和Oracle中可能存在不同的处理行为和返回结果:

1. -- 在GreatSQL和Oracle中均返回1
2. > SELECT INSTR('0.3333', '0.3') FROM DUAL;

4. -- 在GreatSQL和Oracle中均返回1
5. > SELECT INSTR(0.3333, 0.3) FROM DUAL;

7. -- 在GreatSQL返回1
8. -- 在Oracle中返回2
9. > SELECT INSTR('0.3333', 0.3) FROM DUAL;

下面是在GreatSQL中的测试案例:

1. -- 先切换到ORACLE模式
2. greatsql> SET sql_mode = ORACLE;

4. -- 返回:3,第一次出现“l”的位置
5. greatsql> SELECT INSTR('helloworld','l') FROM DUAL;
6. +-------------------------+
7. | INSTR('helloworld','l') |
8. +-------------------------+
9. |                       3 |
10. +-------------------------+

12. -- 返回:4,即“lo”同时出现,第一个字母“l”出现的位置
13. greatsql> SELECT INSTR('helloworld','lo') FROM DUAL;
14. +--------------------------+
15. | INSTR('helloworld','lo') |
16. +--------------------------+
17. |                        4 |
18. +--------------------------+

20. -- 返回:4,在"helloworld"的第2个字符(字符"e")偏移位置开始,查找第二次出现的“l”的位置
21. greatsql> SELECT INSTR('helloworld','l',2,2) FROM DUAL;
22. +-----------------------------+
23. | INSTR('helloworld','l',2,2) |
24. +-----------------------------+
25. |                           4 |
26. +-----------------------------+

28. -- 返回:9,在"helloworld"的偏移位置倒数第1(字符"d")开始,往回查找第一次出现的“l”的位置
29. greatsql> SELECT INSTR('helloworld','l',-1,1) FROM DUAL;
30. +------------------------------+
31. | INSTR('helloworld','l',-1,1) |
32. +------------------------------+
33. |                            9 |
34. +------------------------------+

36. -- 返回:22,即最后IP第四段数据
37. greatsql> SELECT SUBSTR('192.168.0.22', INSTR('192.168.0.22', '.', 1, 3) +1) FROM DUAL;
38. +-------------------------------------------------------------+
39. | SUBSTR('192.168.0.22', INSTR('192.168.0.22', '.', 1, 3) +1) |
40. +-------------------------------------------------------------+
41. | 22                                                          |
42. +-------------------------------------------------------------+

下面这些案例可以自行测试,想想结果是否符合预期:

1. SET sql_mode = ORACLE;
2. SELECT INSTR('a','A') FROM DUAL;
3. SELECT INSTR('thisisatestsentence','t',1,'')  FROM DUAL;
4. SELECT INSTR('thisisatestsentence','t','',1)  FROM DUAL;
5. SELECT INSTR('thisisatestsentence','t',1,2.1)  FROM DUAL;
6. SELECT INSTR('thisisatestsentence','t',1,2.5)  FROM DUAL;
7. SELECT INSTR('thisisatestsentence','t',0.1,1)  FROM DUAL;
8. SELECT INSTR('thisisatestsentence','t',0.5,1)  FROM DUAL;

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx