2020/07/14

取得特定分隔內容資料

1 => 
 FUNCTION temp
  (
    pString IN VARCHAR2,
    pLevel  IN NUMBER,
    pSymble IN CHAR DEFAULT ','
  ) RETURN VARCHAR2 IS
    vString        VARCHAR2(2000);
    vPositionStart NUMBER;
    vPositionEnd   NUMBER;
  BEGIN
    vString        := pSymble || pString || pSymble;
    vPositionStart := instr(vString, pSymble, 1, pLevel);
    vPositionEnd   := instr(vString, pSymble, 1, pLevel + 1);

    RETURN substr(vString, (vPositionStart + 1),
                  vPositionEnd - (vPositionStart + 1));
  EXCEPTION
    WHEN OTHERS THEN
      RETURN NULL;
  END temp;

2 => 
FUNCTION temp
  (
    pString IN VARCHAR2,
    pLevel  IN NUMBER
  ) RETURN VARCHAR2 IS
    vSymble dbms_utility.number_array;
    vLength NUMBER;
    vCount  NUMBER := 0;
    vOutException EXCEPTION;
  BEGIN
    vLength := length(pString);
    FOR i IN 1 .. vLength
    LOOP
      IF SUBSTR(pString, i, 1) = ',' THEN
        vCount := vCount + 1;
        vsymble(vCount) := i;
      END IF;
    END LOOP;

    IF pLevel > vsymble.count + 1 THEN
      RAISE vOutException;
    END IF;

    IF pLevel = 1 THEN
      RETURN SUBSTR(pString, 1, vsymble(1) - 1);
    END IF;

    IF pLevel = vsymble.count + 1 THEN
      RETURN SUBSTR(pString, vsymble(vsymble.count) + 1);
    END IF;
    RETURN SUBSTR(pString, vsymble(pLevel - 1) + 1,
                  vsymble(pLevel) - vsymble(pLevel - 1) - 1);
  EXCEPTION
    WHEN vOutException THEN
      RETURN 'Out of Array';
  END temp;

3 => 
FUNCTION temp
(
  pString IN VARCHAR2,
  pLevel  IN NUMBER,
  pSymble IN CHAR DEFAULT ','
) RETURN VARCHAR2 IS
  vString VARCHAR2(200);
BEGIN
  SELECT str
  INTO   vString
  FROM   (SELECT LEVEL AS pos,
                 regexp_substr(pString, '(.*?)(,|$)', 1, LEVEL, NULL, 1) AS str
          FROM   dual
          CONNECT BY LEVEL <= regexp_count(pString, pSymble) + 1)
  WHERE  pos = pLevel;
  RETURN vString;
EXCEPTION
  WHEN OTHERS THEN
    RETURN NULL;
END temp;

沒有留言: