【原创】oracle函数INSTR的MySQL实现

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
+关注继续查看

一个迁移项目遇到的,MySQL的instr函数只能查找子串是否在父串中,没法按照出现的次数进行查找。 

这里我自己写了一个,以便迁移。当然我这里仅仅针对的是迁移,可能没有完全实现原有函数的细节。


Oracle 里用了几次如下的调用,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
SQL> select instr('This is belong to you, but not to me.','to',1,1) as pos from dual;
                 POS                              
--------------------                              
                  16                              
已用时间:  00: 00: 00.00
SQL> select instr('This is belong to you, but not to me.','to',1,2) as pos from dual;
                 POS                              
--------------------                              
                  32                              
已用时间:  00: 00: 00.00
SQL> select instr('This is belong to you, but not to me.','belong',-1,1) as pos from dual;
                 POS                              
--------------------                              
                   9                              
已用时间:  00: 00: 00.00
SQL> select instr('This is belong to you, but not to me.','belong',-1,2) as pos from dual;
                 POS                              
--------------------                              
                  0                              
已用时间:  00: 00: 00.00



MySQL里效果如下,


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
mysql> select func_instr_oracle('This is belong to you, but not to me.','to',1,1) as pos;
+------+
| pos  |
+------+
|   16 |
+------+
1 row in set (0.00 sec)
mysql> select func_instr_oracle('This is belong to you, but not to me.','to',1,2) as pos;
+------+
| pos  |
+------+
|   32 |
+------+
1 row in set (0.00 sec)
mysql> select func_instr_oracle('This is belong to you, but not to me.','belong',-1,1) as pos;
+------+
| pos  |
+------+
|    9 |
+------+
1 row in set (0.00 sec)
mysql> select func_instr_oracle('This is belong to you, but not to me.','belong',-1,2) as pos;
+------+
| pos  |
+------+
|    0 |
+------+
1 row in set (0.00 sec)



附上函数func_instr_oracle的代码:

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
DELIMITER $$
USE `oracle12c`$$
DROP FUNCTION IF EXISTS `func_instr_oracle`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `func_instr_oracle`(
    f_str VARCHAR(1000), -- Parameter 1
    f_substr VARCHAR(100),  -- Parameter 2
    f_str_pos INT-- Postion
    f_count INT UNSIGNED -- Times
    RETURNS INT(10) UNSIGNED
BEGIN
      -- Created by ytt. Simulating Oracle instr function.
      -- Date 2015/12/5.
      DECLARE INT DEFAULT 0; -- Postion iterator
      DECLARE INT DEFAULT 0; -- Times compare.
      DECLARE v_substr_len INT UNSIGNED DEFAULT 0; -- Length for Parameter 1.
      DECLARE v_str_len INT UNSIGNED DEFAULT 0;  -- Length for Parameter 2.
      SET v_str_len = LENGTH(f_str); 
      SET v_substr_len = LENGTH(f_substr);
      -- Unsigned.
      IF f_str_pos > 0 THEN
        SET i = f_str_pos;
        SET j = 0;
        WHILE i <= v_str_len
        DO
          IF INSTR(LEFT(SUBSTR(f_str,i),v_substr_len),f_substr) > 0 THEN
            SET j = j + 1;
            IF j = f_count THEN
              RETURN i;
            END IF;
          END IF;
          SET i = i + 1;
        END WHILE;
      -- Signed.
      ELSEIF f_str_pos <0 THEN
        SET i = v_str_len + f_str_pos+1;
        SET j = 0;
        WHILE i <= v_str_len AND i > 0 
        DO
          IF INSTR(RIGHT(SUBSTR(f_str,1,i),v_substr_len),f_substr) > 0 THEN
            SET j = j + 1;
            IF j = f_count THEN
              RETURN i - v_substr_len + 1;
            END IF;
          END IF;
          SET i = i - 1;
        END WHILE;
      -- Equal to 0.
      ELSE
        RETURN 0;
      END IF;
      RETURN 0;
    END$$
DELIMITER ;




本文转自 david_yeung 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/1719867,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16小时前
|
Oracle 关系型数据库
oracle 函数 regexp_substr()
oracle函数regexp_substr,测试使用
18 0
|
3月前
|
SQL Oracle 关系型数据库
java实现oracle和mysql的group by分组功能|同时具备max()/min()/sum()/case when 函数等功能
java实现oracle和mysql的group by分组功能|同时具备max()/min()/sum()/case when 函数等功能
|
3月前
|
存储 SQL Oracle
Oracle数据库批量删除表、视图、序列、存储过程、函数脚本
Oracle数据库批量删除表、视图、序列、存储过程、函数脚本
38 0
|
3月前
|
Oracle 关系型数据库
Oracle的 nvl 函数及 nvl2 函数的应用
Oracle的 nvl 函数及 nvl2 函数的应用
65 0
|
3月前
|
Oracle 关系型数据库 MySQL
Mysql 中函数ifnull()实现oracle nvl()函数
Mysql 中函数ifnull()实现oracle nvl()函数
|
4月前
|
Oracle 关系型数据库
Oracle中的wm_concat(column)函数
Oracle中的wm_concat(column)函数
|
4月前
|
SQL Oracle 关系型数据库
Oracle 数据库中 GREATEST() 函数的作用?
Oracle 数据库中 GREATEST() 函数的作用
53 0
|
4月前
|
机器学习/深度学习 人工智能 Oracle
在Oracle中,TO_CHAR()、TO_NUMBER()和TO_DATE()函数的使用方法以及作用
在Oracle中,TO_CHAR()、TO_NUMBER()和TO_DATE()函数的使用方法以及作用
123 0
|
4月前
|
SQL Oracle 关系型数据库
Oracle 数据库中 ,EXTRACT() 函数的作用?
Oracle 数据库中 ,EXTRACT() 函数的作用
78 0
|
4月前
|
Oracle 关系型数据库
Oracle中decode 以及ROW_NUMBER() OVER() 函数等其它相关函数用法
Oracle中decode 以及ROW_NUMBER() OVER() 函数等其它相关函数用法
39 0
推荐文章
更多
推荐镜像
更多