2013-07-16 14 views
6

我有一個地址字段,它可以容納120個字符,並且需要將它分成三個不同的列,每個字符長度爲40個字符。如何將Oracle中的varchar列分成三列

實施例:

Table name: Address 
Column name: Street_Address 
Select Street_Address * from Address 

輸出: 123 Main St North Pole Factory 44, near the rear entrance cross the street and turn left and keep walking straight.

我需要這個地址分成address_1 address_2address_3

所有這三個地址都是varchar(40)數據類型。

所以結果應該是這樣的:

Address_1 
152 Main st North Pole Factory 44, near 

Address_2 
the rear entrance cross the street and 

Address_3 
turn left and keep walking straight. 

請注意,每個地址字段可能需要長達40個字符,必須是整個單詞,不能在半截斷,左無意義。

我正在使用oracle 11i數據庫。

+0

您將在哪裏顯示這些列?你爲什麼不把它分成應用程序級別? – bjan

回答

1

這相當「快速和骯髒」,但我認爲它給出了正確的結果。
我用流水線表,但也許它可以沒有它做...

Here is a sqlfiddle demo

create table t1(id number, adr varchar2(120)) 
/
insert into t1 values(1, '152 Main st North Pole Factory 44, near the rear entrance cross the street and turn left and keep walking straight.') 
/
insert into t1 values(2, '122 Main st Pole Factory 44, near the rear entrance cross the street and turn left and keep walking straight. asdsa') 
/

create or replace type t is object(id number, phrase1 varchar2(40), phrase2 varchar2(40), phrase3 varchar2(40)) 
/
create or replace type t_tab as table of t 
/

create or replace function split_string(id number, str in varchar2) return t_tab 
    pipelined is 

    v_token varchar2(40); 
    v_token_i number := 0; 
    v_cur_len number := 0; 
    v_res_str varchar2(121) := str || ' '; 
    v_p1  varchar2(40); 
    v_p2  varchar2(40); 
    v_p3  varchar2(40); 
    v_p_i  number := 1; 

begin 

    v_token_i := instr(v_res_str, ' '); 

    while v_token_i > 0 loop 

    v_token := substr(v_res_str, 1, v_token_i - 1); 

     if v_cur_len + length(v_token) < 40 then 

     if v_p_i = 1 then 
      v_p1 := v_p1 || ' ' || v_token; 
     elsif v_p_i = 2 then 
      v_p2 := v_p2 || ' ' || v_token; 
     elsif v_p_i = 3 then 
      v_p3 := v_p3 || ' ' || v_token; 
     end if; 

     v_cur_len := v_cur_len + length(v_token) +1; 
    else 
     v_p_i := v_p_i + 1; 

     if v_p_i = 2 then 
      v_p2 := v_p2 || ' ' || v_token; 
     elsif v_p_i = 3 then 
      v_p3 := v_p3 || ' ' || v_token; 
     end if; 

     v_cur_len := length(v_token); 

    end if; 

    v_res_str := substr(v_res_str, v_token_i + 1); 
    v_token_i := instr(v_res_str, ' '); 

    end loop; 

    pipe row(t(id, v_p1, v_p2, v_p3)); 
    return; 
end split_string; 
/

和查詢:

select parts.*, length(PHRASE1), length(PHRASE2), length(PHRASE3) 
from t1, table(split_string(t1.id, t1.adr)) parts 
+0

@ D.L,Ooops,忘記了最後一個詞(沒有空格的地方)更新了我的答案。 (順便說一句,有另一種方法來解決它 - 你可以初始化'v_res_str varchar2(120):= str ||'';' –

+0

@DL,你是什麼意思*在飛行*?你可以使用建議的查詢作爲視圖(可能沒有長度字段...) –

+0

@D。L,我仍然沒有看到問題 - 您也可以添加其他列。看到這個例子http://www.sqlfiddle.com/#!4/eeb18/1 –

2

你可以使用遞歸子查詢分解(遞歸CTE ):

with s (street_address, line, part_address, remaining) as (
    select street_address, 0 as line, 
    null as part_address, street_address as remaining 
    from address 
    union all 
    select street_address, line + 1 as line, 
    case when length(remaining) <= 40 then remaining else 
     substr(remaining, 1, instr(substr(remaining, 1, 40), ' ', -1, 1)) end 
     as part_address, 
    case when length(remaining) <= 40 then null else 
     substr(remaining, instr(substr(remaining, 1, 40), ' ', -1, 1) + 1) end 
     as remaining 
    from s 
) 
cycle remaining set is_cycle to 'Y' default 'N' 
select line, part_address 
from s 
where part_address is not null 
order by street_address, line; 

哪個機智^ h你的數據得出:

 LINE PART_ADDRESS       
---------- ---------------------------------------- 
     1 152 Main st North Pole Factory 44, near 
     2 the rear entrance cross the street and 
     3 turn left and keep walking straight.  

SQL Fiddle demo有兩個地址。

您也可以將這些部分值轉換爲列,我認爲這是您的最終目標,例如,作爲看法:

create or replace view v_address as 
with cte (street_address, line, part_address, remaining) as (
    select street_address, 0 as line, 
    null as part_address, street_address as remaining 
    from address 
    union all 
    select street_address, line + 1 as line, 
    case when length(remaining) <= 40 then remaining else 
     substr(remaining, 1, instr(substr(remaining, 1, 40), ' ', -1, 1)) end 
     as part_address, 
    case when length(remaining) <= 40 then null else 
     substr(remaining, instr(substr(remaining, 1, 40), ' ', -1, 1) + 1) end 
     as remaining 
    from cte 
) 
cycle remaining set is_cycle to 'Y' default 'N' 
select street_address, 
    cast (max(case when line = 1 then part_address end) as varchar2(40)) 
    as address_1, 
    cast (max(case when line = 2 then part_address end) as varchar2(40)) 
    as address_2, 
    cast (max(case when line = 3 then part_address end) as varchar2(40)) 
    as address_3 
from cte 
where part_address is not null 
group by street_address; 

Another SQL Fiddle

值得注意的是,如果street_address長度接近120個字符,它可能不適合整齊地放入3個40個字符的塊中 - 根據纏繞到下一個「行」的單詞的長度, 。這種方法會生成超過3行,但視圖只使用前三行,因此您可能會丟失地址的末尾。您可能希望使字段更長,或者對於這些情況有address_4 ...