zoukankan      html  css  js  c++  java
  • Oracle 自定义实用函数

    一、ctod 字符转为date,  

    create or replace function ctod(str in varchar2)
    return date
    as
    begin
      return to_date(str,'yyyy-MM-dd hh24:mi:ss');
      exception
        when others then 
        return null;
    end;  

    使用示例:
    select ctod('2019-1-17') from dual;

    二、splittb  字符串分割,分割之后返回行。

      

    CREATE OR REPLACE FUNCTION splitTB(
                       str in clob, --待分割的字符串
                       splitchar in varchar2 --分割标志
                )
                return split_table
                IS
                  restStr clob default splitTB.str;--剩余的字符串
                  thisStr varchar2(4000);--取得的当前字符串
                  indexStr int;--临时存放分隔符在字符串中的位置
    
                  v split_table := split_table(); --返回结果
                  v_i int:=0;
                  v_spitchar_len int :=length(splitchar);
                begin
                     dbms_output.put_line(restStr);
                     while length(restStr) != 0
                       LOOP
                         <<top>>
                         indexStr := instr(restStr,splitchar); --从子串中取分隔符的第一个位置
    
                         if indexStr = 0 and length(restStr) != 0  then--在剩余的串中找不到分隔符
                            begin
                              v_i:=v_i+1;
                              v.extend;
                              v(v.count) := split_arr(v_i,trim(Reststr));
                              return v;
                            end;
                         end if;
                         if indexStr = 1 then---第一个字符便为分隔符,此时去掉分隔符
                            begin
                                 restStr := substr(restStr,v_spitchar_len);
                                 goto   top;
                            end;
                         end if;
    
                         if length(restStr) = 0 or restStr is null then
                            return v;
                         end if;
                        v_i:=v_i+1;
                         v.extend;
                         thisStr := trim(substr(restStr,1,indexStr - 1)); --取得当前的字符串
                         restStr := substr(restStr,indexStr + v_spitchar_len);---取剩余的字符串
                         v(v.count) := split_arr(v_i,thisStr);
                       END LOOP;
                     return v;
                end;

    使用示例:
    select * from table(splittb('a1,b2,cc',','));

    返回:(两列,第一列I序号,每二列cstr分割的值

    I----------CSTR--------------------------------------------------------------------------------
    1    a1

    2    b2

    3    cc



      

  • 相关阅读:
    2020/10/10周总结
    2020/10/02周总结
    2020/9/28周总结
    第十二周总结
    第十一周总结
    人月神话阅读笔记03
    人月神话阅读笔记02
    人月神话阅读笔记01
    冲刺一8
    冲刺一7
  • 原文地址:https://www.cnblogs.com/KevinMO/p/10287193.html
Copyright © 2011-2022 走看看