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


§ 1. 语法

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

§ 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时,会舍弃整数部分)。例如:

-- 在GreatSQL中得到1
greatsql> SELECT INSTRB('0.3333', 0.3) FROM DUAL;
+-----------------------+
| INSTRB('0.3333', 0.3) |
+-----------------------+
|                     1 |
+-----------------------+

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

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

       0.3 '0.
---------- ---
        .3 0.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

§ 4. 示例

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

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

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

greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S');
+----------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S') |
+----------------------------------------------+
|                                            6 |
+----------------------------------------------+

greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2);
+----------------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 2) |
+----------------------------------------------------+
|                                                 27 |
+----------------------------------------------------+

greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1);
+--------------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S', -1) |
+--------------------------------------------------+
|                                               27 |
+--------------------------------------------------+

greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2);
+-----------------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S', -1, 2) |
+-----------------------------------------------------+
|                                                   6 |
+-----------------------------------------------------+

-- 参数中的小数转整数
greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6);
+------------------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S', 1, 1.6) |
+------------------------------------------------------+
|                                                    6 |
+------------------------------------------------------+

greatsql> SELECT INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1);
+------------------------------------------------------+
| INSTRB('GreatSQL is a branch of MySQL', 'S', 0.6, 1) |
+------------------------------------------------------+
|                                                    0 |
+------------------------------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

greatsql-wx