功能返回需要适当的情况

时间:2017-12-16 06:14:19

标签: sql oracle

我需要一个使列名具有值

的函数
values(

feature plush

^Ido ^3d

^Imc Toys

^Tmnt - ^Lpl

^Bo-po

)
像这样

并像这样返回

(

Feature Plush

IDO 3D

IMC Toys

TMNT - LPL

BO-PO

)

请帮帮我

1 个答案:

答案 0 :(得分:0)

以下功能适用于您的要求。

CREATE OR REPLACE Function DOING_XX (String_In IN Varchar2) Return Varchar2 Is

i Number(2);
string_out varchar2(1000) := '';

Begin

    FOR i in 1..((LENGTH(String_In) - LENGTH(REPLACE(String_In,' ','')) ) + 1)
    LOOP

        If i != 1 
        Then 
            string_out := String_out||' ';
        End If;

        If( SubStr( REGEXP_SUBSTR(String_In,'[^ ]+',1,i) , 1 , 1) = '^' )
        Then
            string_out := String_out || UPPER(SubStr(REGEXP_SUBSTR(String_In,'[^ ]+',1,i),2));
        Else
            string_out := String_out || INITCAP(REGEXP_SUBSTR(String_In,'[^ ]+',1,i));
        End If;

    End LOOP;

    Return trim(string_out);

End;
/

with x as(
select 'feature    plush' a from dual union all
select '^Ido ^3d' a from dual union all
select '^Imc Toys' a from dual union all
select '^Tmnt - ^Lpl' a from dual union all
select '^Bo-po' a from dual
)
select DOING_XX(a) from x

我想告诉你,它会返回不需要的尾随空格,以防两个空间的数量有效!= 1.为此你需要一个更好的regexp_substr来捕获它。 Return trim(string_out)是对此的临时修复。