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 字符串。

注意: 当参数 string 和 sub_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. +-----------------------+

9. -- 在Oracle中得到2
10. SQL> SELECT INSTRB('0.3333', 0.3) FROM DUAL;

12. INSTRB('0.3333',0.3)
13. --------------------
14. 2
15. -- 在Oracle中,小数位和字符串表示方式分别如下
16. SQL> SELECT 0.3, '0.3' FROM DUAL;

18. 0.3 '0.
19. ---------- ---
20. .3 0.3

4. 示例

1. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高');
2. +---------------------------------------------------------------------------------------------------+
3. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高')                          |
4. +---------------------------------------------------------------------------------------------------+
5. |                                                                                                15 |
6. +---------------------------------------------------------------------------------------------------+

8. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 1);
9. +---------------------------------------------------------------------------------------------------------+
10. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 1)                          |
11. +---------------------------------------------------------------------------------------------------------+
12. |                                                                                                      15 |
13. +---------------------------------------------------------------------------------------------------------+

15. greatsql> SELECT INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 2);
16. +---------------------------------------------------------------------------------------------------------+
17. | INSTRB('GreatSQL具备高可用、高性能、高兼容、高安全等多个核心特性', '高', 1, 2)                          |
18. +---------------------------------------------------------------------------------------------------------+
19. |                                                                                                      27 |
20. +---------------------------------------------------------------------------------------------------------+

22. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S');
23. +----------------------------------------------+
24. | INSTRB('GreatSQL is a branch of MySQL', 'S') |
25. +----------------------------------------------+
26. |                                            6 |
27. +----------------------------------------------+

29. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2);
30. +----------------------------------------------------+
31. | INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2) |
32. +----------------------------------------------------+
33. |                                                 27 |
34. +----------------------------------------------------+

36. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1);
37. +--------------------------------------------------+
38. | INSTRB('GreatSQL is a branch of MySQL', 'S', -1) |
39. +--------------------------------------------------+
40. |                                               27 |
41. +--------------------------------------------------+

43. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2);
44. +-----------------------------------------------------+
45. | INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2) |
46. +-----------------------------------------------------+
47. |                                                   6 |
48. +-----------------------------------------------------+

50. -- 参数中的小数转整数
51. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6);
52. +------------------------------------------------------+
53. | INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6) |
54. +------------------------------------------------------+
55. |                                                    6 |
56. +------------------------------------------------------+

58. greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1);
59. +------------------------------------------------------+
60. | INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1) |
61. +------------------------------------------------------+
62. |                                                    0 |
63. +------------------------------------------------------+

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx