If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > PL/SQL help - split an address

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-28-04, 14:16
liftapia liftapia is offline
Registered User
 
Join Date: Jan 2004
Posts: 19
Exclamation PL/SQL help - split an address

I have a single address column that i want to split.
For example I have an address with carriage returns like:

address
--------------------------------
Administration Tech Services

1234 Elm Avenue

West Building


I would like a SELECT query to split this address column into 3 like:
address address2 address3
---------- ----------- -----------
Administration Tech Services 1234 Elm Avenue West Building
Reply With Quote
  #2 (permalink)  
Old 12-28-04, 16:04
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

You may need to create a function like this:
Code:
Create Or Replace Function Get_Addr
(P_Line Number, P_Addr Varchar2)
Return Varchar2 Is
Type Addr_Typ Is Table Of Varchar2(1000);
V_Addr Addr_Typ;
V_Tmp Varchar2(1000);
I Pls_Integer;
J Pls_Integer;
K Pls_Integer;
L Pls_Integer;
Begin
  V_Tmp:=Rtrim(P_Addr)||Chr(10);
  For I In 1..P_Line Loop
    V_Addr(I):='';
  End Loop;
  I:=0;
  L:=Length(V_Tmp);
  While (L > 0)
  Loop
    K:=Instr(V_Tmp,Chr(10));
    If K = 0 Then
      Exit;
    End If;
    J:= K-1;
    If J > 0 Then
      I:=I+1;
      V_Addr(I):= Substr(V_Tmp,1,J);
      V_Tmp:=Rtrim(Substr(V_Tmp||' ',K+1));
    End If;
    L:=Length(V_Tmp);
  End Loop;
  Return V_Addr(P_Line);
End;
/
And use it like:
Code:
UPDATE MyAddrTab
     SET Address2=Get_Addr(2,Address)
         , Address3=Get_Addr(3,Address);
COMMIT;
UPDATE MyAddrTab
     SET Address=Get_Addr(1,Address);
COMMIT;


PS: You will need to validate parameters!
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On