格式化行pl / sql中的数据

时间:2017-01-27 07:04:44

标签: plsql

我应该格式化列的数据。 数据看起来像

  

test_segment_withcountry-国家:巴巴多斯,阿富汗,阿尔及利亚,巴哈马,阿塞拜疆,奥地利;待办事项   您想要包含Oracle 2000 C级联系人:否;标题级别:Chief   数据官,首席客户服务主任;这些联系人是否   用于电子邮件活动:是; test_all_region - 您要包括吗?   Oracle 2000 C级联系人:是;区域:NA,APAC,EMEA,JAPAN,LAD; Will   这些联系人用于电子邮件活动:否;

这是单个字段的值。每个分号后,它应该转到下一行。

我试过了, 测试为

(select 'test_segment_withcountry-Country:BARBADOS,AFGHANISTAN,ALGERIA,BAHAMAS,AZERBAIJAN,AUSTRIA;Do you want to include Oracle 2000 C-Level Contacts:No;Title Level:Chief Data Officer,Chief Customer Service Officer;Will these Contacts be used in Email Campaign:Yes;test_all_region-Do you want to include Oracle 2000 C-Level Contacts:Yes;Region:NA,APAC,EMEA,JAPAN,LAD;Will these Contacts be used in Email Campaign:No;' col from rp_segment_master where sm_id=254)
    select regexp_substr(col, '[^;]+', 1, level) result
    from test
    connect by level <= length(regexp_replace(col, '[^;]+')) + 1; 

但这将有不同的行,但我希望它是一个单独的字段。 我知道我在这里很暧昧。

2 个答案:

答案 0 :(得分:0)

对于oracle sql中的新行,请使用 chr(10)

您可以替换&#39; &#39;符号 chr(10)

select replace('BARBADOS,AFGHANISTAN,ALGERIA,BAHAMAS', ',', chr(10))  from dual

enter image description here

或者,如果你想拆分,那么拆分功能将帮助你。它会给出行结果。如果您想将其显示在一行中,则可以使用 LISTAGG 功能。

create or replace type split_tbl as table of varchar2(32767);


create or replace function split
(
    p_list varchar2,
    p_del varchar2 := ','
) return split_tbl pipelined
is
    l_idx    pls_integer;
    l_list    varchar2(32767) := p_list;
    l_value    varchar2(32767);
begin
    loop
        l_idx := instr(l_list,p_del);
        if l_idx > 0 then
            pipe row(substr(l_list,1,l_idx-1));
            l_list := substr(l_list,l_idx+length(p_del));

        else
            pipe row(l_list);
            exit;
        end if;
    end loop;
    return;
end split;

使用此功能,您可以运行如下查询:

 select * from table(split('one,two,three'));

这里有一些字符串聚合示例:

https://oracle-base.com/articles/misc/string-aggregation-techniques

答案 1 :(得分:0)

你需要

Replace;您只需要了解是否需要使用chr(10)chr(13)||chr(10),具体取决于您是否使用Windows。

Tom说:

  

在Unix上,LINEFEED(chr(10))是&#34;行的结尾&#34;标记。比尔很久以前   决定在DOS上,CARRIAGE RETURN / LINEFEED(chr(13)|| chr(10))   应该是行标记的结尾。

所以,你可能需要:

with test(s) as ( select 'test_segment_withcountry-Country:BARBADOS,AFGHANISTAN,ALGERIA,BAHAMAS,AZERBAIJAN,AUSTRIA;Do you want to include Oracle 2000 C-Level Contacts:No;Title Level:Chief Data Officer,Chief Customer Service Officer;Will these Contacts be used in Email Campaign:Yes;test_all_region-Do you want to include Oracle 2000 C-Level Contacts:Yes;Region:NA,APAC,EMEA,JAPAN,LAD;Will these Contacts be used in Email Campaign:No;' from dual)
select replace (s, ';', chr(10))
from test;

with test(s) as ( select 'test_segment_withcountry-Country:BARBADOS,AFGHANISTAN,ALGERIA,BAHAMAS,AZERBAIJAN,AUSTRIA;Do you want to include Oracle 2000 C-Level Contacts:No;Title Level:Chief Data Officer,Chief Customer Service Officer;Will these Contacts be used in Email Campaign:Yes;test_all_region-Do you want to include Oracle 2000 C-Level Contacts:Yes;Region:NA,APAC,EMEA,JAPAN,LAD;Will these Contacts be used in Email Campaign:No;' from dual)
select replace (s, ';', chr(13) || chr(10))
from test;