oracle 获取字符串中的字段值

CREATE OR REPLACE FUNCTION colcount (pvar_s VARCHAR2, pvar_mask VARCHAR2)
   RETURN INT
IS
   pvar_i       INT;
   pvar_len     INT;
   pvar_count   INT;
   pvar_inst    int;
BEGIN
   pvar_i := 0;
   pvar_len := LENGTH (pvar_s);
   pvar_count := 0;
 
   IF pvar_len = 0
   THEN
      RETURN pvar_count;
   END IF;
 
   WHILE pvar_i < pvar_len
   LOOP
      pvar_i := pvar_i + 1;
      pvar_inst := instr(pvar_s,pvar_mask,pvar_i);
      if pvar_inst>0 then
         pvar_i := pvar_inst;
         pvar_count := pvar_count + 1;
      end if;
   END LOOP;
 
   RETURN pvar_count + 1;
END colcount;


CREATE OR REPLACE FUNCTION getcol (
   pvar_s       VARCHAR2
 , pvar_index   INT
 , pvar_mask    VARCHAR2
)
   RETURN VARCHAR2
IS
   pvar_i        INT;
   pvar_result   VARCHAR2 (4096);
BEGIN
   pvar_i := 0;
 
   IF pvar_index > colcount (pvar_s, pvar_mask)
   THEN
      RETURN ' ';
   END IF;
 
   pvar_result := pvar_mask || pvar_s || pvar_mask;
 
   WHILE pvar_i <= pvar_index
   LOOP
      pvar_i := pvar_i + 1;
      pvar_result :=
         SUBSTR (pvar_result
               , INSTR (pvar_result, pvar_mask) + 1
               , LENGTH (pvar_result)
                );
   END LOOP;
 
   IF INSTR (pvar_result, pvar_mask) > 0
   THEN
      pvar_result :=
                   SUBSTR (pvar_result, 1, INSTR (pvar_result, pvar_mask) - 1);
   END IF;
 
   RETURN pvar_result;
END getcol;

CREATE OR REPLACE FUNCTION colindex(
   pvar_s        VARCHAR2
 , pvar_substr   VARCHAR2
 , pvar_mask     VARCHAR2
)
   RETURN INT
IS
   pvar_i        INT;
   pvar_count    INT;
   pvar_result   INT;
BEGIN
   pvar_i := 0;
   pvar_result := -1;
   pvar_count := colcount (pvar_s, pvar_mask);
 
   WHILE pvar_i < pvar_count
   LOOP
      IF getcol (pvar_s, pvar_i, pvar_mask) = pvar_substr
      THEN
         pvar_result := pvar_i;
         EXIT;
      END IF;
 
      pvar_i := pvar_i + 1;
   END LOOP;
 
   RETURN pvar_result;
END colindex;

CREATE OR REPLACE FUNCTION strFieldByName
                                         (sRow        VARCHAR2,
                                          sFieldName  VARCHAR2,
                                          sFieldMask   VARCHAR2,
                                          sValueMask  VARCHAR)
  RETURN VARCHAR2 IS
  pvar_i            INT;
  pvar_S            VARCHAR2(4000);
  pvar_Result       VARCHAR2(4000);
  pvar_count        int;
  pvar_LenFieldName integer;
  pvar_sub          varchar2(4000);
  pvar_sRow         varchar2(4000);
  pvar_stmp         varchar2(4000);
  pvar_iCopyCount   int;
BEGIN
  pvar_i    := 0;
  pvar_sRow := sRow;
  pvar_sub  := substr(pvar_sRow, 0, 1);
  if pvar_sub <> nvl(sFieldMask, ';') then
    pvar_sRow := nvl(sFieldMask, ';') || sRow;
  end if;
 
  pvar_sub := substr(pvar_sRow, Length(sRow), 1);
  if pvar_sub <> nvl(sFieldMask, ';') then
    pvar_sRow := pvar_sRow || nvl(sFieldMask, ';');
  end if;
 
  pvar_stmp         := nvl(sFieldMask, ';') || sFieldName ||
                       nvl(sValueMask, '=');
  pvar_LenFieldName := instr(lower(pvar_sRow), lower(pvar_stmp)); --fieldname不分大小写
  if pvar_LenFieldName > 0 then
    pvar_iCopyCount := Instr(pvar_sRow,
                             nvl(sFieldMask, ';'),
                             pvar_LenFieldName + Length(pvar_stmp));
   
    --找到这个field的最后一个;号
    pvar_i := Instr(pvar_sRow, nvl(sValueMask, '='), pvar_iCopyCount + 1);
    while (Instr(pvar_sRow, nvl(sFieldMask, ';'), pvar_iCopyCount + 1) between 1 and
          pvar_i - 1) loop
      pvar_iCopyCount := Instr(pvar_sRow,
                               nvl(sFieldMask, ';'),
                               pvar_iCopyCount + 1);
    end loop;
   
    pvar_Result := substr(pvar_sRow,
                          pvar_LenFieldName + Length(pvar_stmp),
                          pvar_iCopyCount - Length(pvar_stmp) -
                          pvar_LenFieldName);
   
    goto break;
  end if;
  <<break>>
  RETURN pvar_result;
END strfieldbyname;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值