Home » SQL & PL/SQL » SQL & PL/SQL » help reqd..vry impt..
help reqd..vry impt.. [message #36243] Tue, 13 November 2001 04:10 Go to next message
Yat
Messages: 4
Registered: November 2001
Junior Member
hi all,
i have a procedure to which i pass a string which contains certain numbers delimeted using ^.
in the porcedure i extract the number from the string using following logic..
str is the input string varchar
-----------------------------------------
I:= INSTR(STR,'^',1,1);
WHILE I<>0 LOOP
I:= INSTR(STR,'^',1,N);
J:= INSTR(STR,'^',1,n+1);
V_CHAR(CNT):=SUBSTR(STR,I+1,J-I-1);
N:=N+1;
DBMS_OUTPUT.PUT_LINE('I'||I);
DBMS_OUTPUT.PUT_LINE('J'||J);
DBMS_OUTPUT.PUT_LINE('VCHAR'||V_CHAR(CNT));
CNT:=CNT +1;
END LOOP;
--------------------------------------------
consider the input string str is '^261065^261069^'
then the output is following..
------------------
I1
J8
VCHAR261065
I8
J15
VCHAR261069
I15
J0
VCHAR
I0
J0
VCHAR
-------------------
here the i am going through loop twice even after the values are being
retrived from the string..
can any body help me..
please reply ASAP..i am stuck up here for longtime..
regards,
yat

----------------------------------------------------------------------
Re: help reqd..vry impt.. [message #36244 is a reply to message #36243] Tue, 13 November 2001 04:44 Go to previous message
Rob Baillie
Messages: 33
Registered: November 2001
Member
You're getting that extra loop because the exit condition (While...) is evaluated at the start of the loop.

Since you are updating 'I' to the new value AFTER the check, the code will run through the loop one last time.

Try changing to an 'EXIT WHEN' just after the update to 'I' (see below).

Also, try reading "http://otn.oracle.com/doc/server.804/a58236/03_struc.htm#573", it gives you details on the (fairly standard) loop structures available to use, and how they will behave.

New example:

I:= INSTR(STR,'^',1,1);
LOOP
I:= INSTR(STR,'^',1,N);
EXIT WHEN I=0;
J:= INSTR(STR,'^',1,n+1);
V_CHAR:=SUBSTR(STR,I+1,J-I-1);
N:=N+1;
DBMS_OUTPUT.PUT_LINE('I'||I);
DBMS_OUTPUT.PUT_LINE('J'||J);
DBMS_OUTPUT.PUT_LINE('VCHAR'||V_CHAR);
CNT:=CNT +1;
END LOOP;

Using the same string, gives the output:

I1
J1
VCHAR
I1
J8
VCHAR261065
I8
J15
VCHAR261069
I15
J0
VCHAR

----------------------------------------------------------------------
Previous Topic: Re: how to get day of week without using oracle specific keywords
Next Topic: INSERTING TABLE INFORMATION
Goto Forum:
  


Current Time: Sat Apr 20 09:11:15 CDT 2024