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. -- GreatSQLOracle中均返回1
  2. > SELECT INSTR('0.3333', '0.3') FROM DUAL;
  3. -- GreatSQLOracle中均返回1
  4. > SELECT INSTR(0.3333, 0.3) FROM DUAL;
  5. -- GreatSQL返回1
  6. -- Oracle中返回2
  7. > SELECT INSTR('0.3333', 0.3) FROM DUAL;

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

  1. -- 先切换到ORACLE模式
  2. greatsql> SET sql_mode = ORACLE;
  3. -- 返回:3,第一次出现“l”的位置
  4. greatsql> SELECT INSTR('helloworld','l') FROM DUAL;
  5. +-------------------------+
  6. | INSTR('helloworld','l') |
  7. +-------------------------+
  8. | 3 |
  9. +-------------------------+
  10. -- 返回:4,即“lo”同时出现,第一个字母“l”出现的位置
  11. greatsql> SELECT INSTR('helloworld','lo') FROM DUAL;
  12. +--------------------------+
  13. | INSTR('helloworld','lo') |
  14. +--------------------------+
  15. | 4 |
  16. +--------------------------+
  17. -- 返回:4,在"helloworld"的第2个字符(字符"e")偏移位置开始,查找第二次出现的“l”的位置
  18. greatsql> SELECT INSTR('helloworld','l',2,2) FROM DUAL;
  19. +-----------------------------+
  20. | INSTR('helloworld','l',2,2) |
  21. +-----------------------------+
  22. | 4 |
  23. +-----------------------------+
  24. -- 返回:9,在"helloworld"的偏移位置倒数第1(字符"d")开始,往回查找第一次出现的“l”的位置
  25. greatsql> SELECT INSTR('helloworld','l',-1,1) FROM DUAL;
  26. +------------------------------+
  27. | INSTR('helloworld','l',-1,1) |
  28. +------------------------------+
  29. | 9 |
  30. +------------------------------+
  31. -- 返回:22,即最后IP第四段数据
  32. greatsql> SELECT SUBSTR('192.168.0.22', INSTR('192.168.0.22', '.', 1, 3) +1) FROM DUAL;
  33. +-------------------------------------------------------------+
  34. | SUBSTR('192.168.0.22', INSTR('192.168.0.22', '.', 1, 3) +1) |
  35. +-------------------------------------------------------------+
  36. | 22 |
  37. +-------------------------------------------------------------+

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

  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