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


1. 语法

  1. INSTRB( string, sub_string [, start_position [, nth_appearance ] ] )

2. 定义和用法

参数:

  • string,指定需要在其中搜索子字符串的字符串。
  • sub_string,指定需要搜索的子字符串。
  • start_position,可选参数,指定字符串从字节搜索开始的位置。默认值为1。INSTRB()函数从字符串末尾算起start_position的字节数,如果指定的值为负数,则向字符串开头进行搜索。
  • nth_appearance,可选参数,指定子字符串的第n个匹配。预设值为1。

3. Oracle兼容说明

函数 INSTRB() 的作用是按字节在 string 字符串中查找 sub_string 字符串。

注意: 当参数 stringsub_string 的类型不一致时,在GreatSQL和Oracle中的类型转换处理方式可能存在不同,Oracle有些会进行特殊转换处理(如参数sub_string为小数类型,在转换为字符类型时,当整数部分为0时,会舍弃整数部分)。例如:

  1. -- GreatSQL中得到1
  2. greatsql> SELECT INSTRB('0.3333', 0.3) FROM DUAL;
  3. +-----------------------+
  4. | INSTRB('0.3333', 0.3) |
  5. +-----------------------+
  6. | 1 |
  7. +-----------------------+
  8. -- Oracle中得到2
  9. SQL> SELECT INSTRB('0.3333', 0.3) FROM DUAL;
  10. INSTRB('0.3333',0.3)
  11. --------------------
  12. 2
  13. -- Oracle中,小数位和字符串表示方式分别如下
  14. SQL> SELECT 0.3, '0.3' FROM DUAL;
  15. 0.3 '0.
  16. ---------- ---
  17. .3 0.3

4. 示例

  1. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高');
  2. +---------------------------------------------------------------------------------------------------+
  3. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高') |
  4. +---------------------------------------------------------------------------------------------------+
  5. | 15 |
  6. +---------------------------------------------------------------------------------------------------+
  7. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 1);
  8. +---------------------------------------------------------------------------------------------------------+
  9. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 1) |
  10. +---------------------------------------------------------------------------------------------------------+
  11. | 15 |
  12. +---------------------------------------------------------------------------------------------------------+
  13. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 2);
  14. +---------------------------------------------------------------------------------------------------------+
  15. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 2) |
  16. +---------------------------------------------------------------------------------------------------------+
  17. | 27 |
  18. +---------------------------------------------------------------------------------------------------------+
  19. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S');
  20. +----------------------------------------------+
  21. | INSTRB('GreatSQL is a branch of MySQL', 'S') |
  22. +----------------------------------------------+
  23. | 6 |
  24. +----------------------------------------------+
  25. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2);
  26. +----------------------------------------------------+
  27. | INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2) |
  28. +----------------------------------------------------+
  29. | 27 |
  30. +----------------------------------------------------+
  31. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1);
  32. +--------------------------------------------------+
  33. | INSTRB('GreatSQL is a branch of MySQL', 'S', -1) |
  34. +--------------------------------------------------+
  35. | 27 |
  36. +--------------------------------------------------+
  37. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2);
  38. +-----------------------------------------------------+
  39. | INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2) |
  40. +-----------------------------------------------------+
  41. | 6 |
  42. +-----------------------------------------------------+
  43. -- 参数中的小数转整数
  44. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6);
  45. +------------------------------------------------------+
  46. | INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6) |
  47. +------------------------------------------------------+
  48. | 6 |
  49. +------------------------------------------------------+
  50. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1);
  51. +------------------------------------------------------+
  52. | INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1) |
  53. +------------------------------------------------------+
  54. | 0 |
  55. +------------------------------------------------------+

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx