剥离oracle

时间:2017-05-05 14:32:57

标签: oracle plsql oracle11g

我有以下组合作为输入,请帮我一个函数返回输出变量,如下所述

COMPANY-ID_ABC_123          ==> This should give three outputs  (COMPANY-ID,ABC,123)
COMPANY-ID                  ==> this should give give three outputs such as (COMPANY-ID,null,null)
COMPANY-ID-FLAG             ==> this should give give three outputs such as (COMPANY-ID,FLAG,null)
COMPANY-ID-FLAG-ES      ==> this should give give three outputs such as (COMPANY-ID,FLAG,ES)
COMPANY-ID-ES               ==> this should give give three outputs such as (COMPANY-ID,ES,null)
IOB-2003                ==> if i give any other string other than the above , it has to just display (IOB-2003,null,null)

1 个答案:

答案 0 :(得分:0)

假设这些是唯一要考虑的值,您可以使用多个if-elsif-end语句对其进行硬编码。否则你必须解析输入字符串。如果我必须解析它,我会首先从COMPANY-ID中删除连字符,然后在' - '上解析字符串。和/或' _'。

CREATE OR REPLACE PROCEDURE getVal
    ( string_in    IN varchar2,
      string_out1 OUT varchar2,
      string_out2 OUT varchar2,
      string_out3 OUT varchar2 )
AS
   string_out1 := 'IOB-2003';
   string_out2 := 'null';
   string_out3 := 'null';

BEGIN

    IF string_in = 'COMPANY-ID_ABC_123' THEN
        string_out1 := 'COMPANY-ID';
        string_out2 := 'ABC';
        string_out3 := '123';

    ELSIF string_in = 'COMPANY-ID' THEN
        string_out1 := 'COMPANY-ID';

    ELSIF string_in = 'COMPANY-ID-FLAG' THEN
        string_out1 := 'COMPANY-ID';
        string_out2 := 'FLAG';

    ELSIF string_in = 'COMPANY-ID-FLAG-ES' THEN
        string_out1 := 'COMPANY-ID';
        string_out2 := 'FLAG';
        string_out3 := 'ES';

    ELSIF string_in = 'COMPANY-ID-ES' THEN
        string_out1 := 'COMPANY-ID';
        string_out2 := 'ES';

    END IF;

END;