PostgreSQL仿照Oracle的instr函数

转自:http://www.myexception.cn/operating-system/480929.html


PostgreSQL模仿Oracle的instr函数

[sql]  view plain  copy
  1. --  
  2. -- instr functions that mimic Oracle's counterpart  
  3. -- Syntax: instr(string1, string2, [n], [m]) where [] denotes optional parameters.  
  4. --  
  5. -- Searches string1 beginning at the nth character for the mth occurrence  
  6. -- of string2.  If n is negative, search backwards.  If m is not passed,  
  7. -- assume 1 (search starts at first character).  
  8. --  
  9.   
  10. CREATE FUNCTION instr(varcharvarcharRETURNS integer AS $$  
  11. DECLARE  
  12.     pos integer;  
  13. BEGIN  
  14.     pos:= instr($1, $2, 1);  
  15.     RETURN pos;  
  16. END;  
  17. $$ LANGUAGE plpgsql STRICT IMMUTABLE;  
  18.   
  19.   
  20. CREATE FUNCTION instr(string varchar, string_to_search varchar, beg_index integer)  
  21. RETURNS integer AS $$  
  22. DECLARE  
  23.     pos integer NOT NULL DEFAULT 0;  
  24.     temp_str varchar;  
  25.     beg integer;  
  26.     length integer;  
  27.     ss_length integer;  
  28. BEGIN  
  29.     IF beg_index > 0 THEN  
  30.         temp_str := substring(string FROM beg_index);  
  31.         pos := position(string_to_search IN temp_str);  
  32.   
  33.         IF pos = 0 THEN  
  34.             RETURN 0;  
  35.         ELSE  
  36.             RETURN pos + beg_index - 1;  
  37.         END IF;  
  38.     ELSE  
  39.         ss_length := char_length(string_to_search);  
  40.         length := char_length(string);  
  41.         beg := length + beg_index - ss_length + 2;  
  42.   
  43.         WHILE beg > 0 LOOP  
  44.             temp_str := substring(string FROM beg FOR ss_length);  
  45.             pos := position(string_to_search IN temp_str);  
  46.   
  47.             IF pos > 0 THEN  
  48.                 RETURN beg;  
  49.             END IF;  
  50.   
  51.             beg := beg - 1;  
  52.         END LOOP;  
  53.   
  54.         RETURN 0;  
  55.     END IF;  
  56. END;  
  57. $$ LANGUAGE plpgsql STRICT IMMUTABLE;  
  58.   
  59.   
  60. CREATE FUNCTION instr(string varchar, string_to_search varchar,  
  61.                       beg_index integer, occur_index integer)  
  62. RETURNS integer AS $$  
  63. DECLARE  
  64.     pos integer NOT NULL DEFAULT 0;  
  65.     occur_number integer NOT NULL DEFAULT 0;  
  66.     temp_str varchar;  
  67.     beg integer;  
  68.     i integer;  
  69.     length integer;  
  70.     ss_length integer;  
  71. BEGIN  
  72.     IF beg_index > 0 THEN  
  73.         beg := beg_index;  
  74.         temp_str := substring(string FROM beg_index);  
  75.   
  76.         FOR i IN 1..occur_index LOOP  
  77.             pos := position(string_to_search IN temp_str);  
  78.   
  79.             IF i = 1 THEN  
  80.                 beg := beg + pos - 1;  
  81.             ELSE  
  82.                 beg := beg + pos;  
  83.             END IF;  
  84.   
  85.             temp_str := substring(string FROM beg + 1);  
  86.         END LOOP;  
  87.   
  88.         IF pos = 0 THEN  
  89.             RETURN 0;  
  90.         ELSE  
  91.             RETURN beg;  
  92.         END IF;  
  93.     ELSE  
  94.         ss_length := char_length(string_to_search);  
  95.         length := char_length(string);  
  96.         beg := length + beg_index - ss_length + 2;  
  97.   
  98.         WHILE beg > 0 LOOP  
  99.             temp_str := substring(string FROM beg FOR ss_length);  
  100.             pos := position(string_to_search IN temp_str);  
  101.   
  102.             IF pos > 0 THEN  
  103.                 occur_number := occur_number + 1;  
  104.   
  105.                 IF occur_number = occur_index THEN  
  106.                     RETURN beg;  
  107.                 END IF;  
  108.             END IF;  
  109.   
  110.             beg := beg - 1;  
  111.         END LOOP;  
  112.   
  113.         RETURN 0;  
  114.     END IF;  
  115. END;  
  116. $$ LANGUAGE plpgsql STRICT IMMUTABLE;  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值