Home » SQL & PL/SQL » SQL & PL/SQL » Spliting of String (Oracle 10g)
Spliting of String [message #632769] Thu, 05 February 2015 09:29 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Hi All,

If Parameter (P_In_String) Of Function Split Is Changed To Clob It Is Returning One Extra Count
,While If The Parameter Is Varchar2 Is Returning Correct Output . Why Is This Occuring.Please Help Me.

create or replace PACKAGE           test_pkg
IS
  TYPE t_array
    IS TABLE OF VARCHAR2(32767) INDEX BY BINARY_INTEGER;
    
  FUNCTION SPLIT(
    p_in_string clob,---changed to varchar2 means one less value will be returned
    p_delim     VARCHAR2)
  RETURN T_ARRAY;
end;  

CREATE OR REPLACE 
PACKAGE BODY            test_pkg as 
 FUNCTION Split(p_in_string clob,
                 p_delim     VARCHAR2)
  RETURN T_ARRAY
  IS
    i       NUMBER := 0;
    pos     NUMBER := 0;
--    lv_str  VARCHAR2(2000) := p_in_string;
lv_str  clob := p_in_string;
    strings T_ARRAY;
  BEGIN
      -- determine first chuck of string
      pos := Instr(lv_str, p_delim, 1, 1);

      --if only string is there without any delimeter then it is returning that string itself.
      if pos=0 and lv_str is not null then
        Strings(1):=lv_str;
      end if;

      WHILE ( pos != 0 ) LOOP
  
          i := i + 1;

          Strings(i) := Substr(lv_str, 1, pos - 1);

          lv_str := Substr(lv_str, pos + 1, Length(lv_str));

          pos := Instr(lv_str, p_delim, 1, 1);

          IF pos = 0 THEN
            Strings(i + 1) := lv_str;
          END IF;
      END LOOP;

      RETURN strings;
  END SPLIT;
  end test_pkg;
  
  
--testing after changing clob (3 count) /varchar2(2 count) to  p_in_string of split function
  DECLARE
t_priority  test_pkg.t_array;
BEGIN
t_priority   := test_pkg.SPLIT('~1~', '~');
dbms_output.put_line(t_priority.count);
END;


Regards,
Nathan

[Updated on: Thu, 05 February 2015 09:30]

Report message to a moderator

Re: Spliting of String [message #632776 is a reply to message #632769] Thu, 05 February 2015 09:56 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
?? I can't see any output. Please post it, else it is still an incomplete test case.
Re: Spliting of String [message #632783 is a reply to message #632769] Thu, 05 February 2015 11:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Add dbms_output in your code at different places to display different variables and so you'll know.
Why should we debug your code for you?

Use our help but do not abuse of it.

Re: Spliting of String [message #632813 is a reply to message #632783] Fri, 06 February 2015 00:23 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks for reply,

Currently the parameter datatype is clob, so the output is coming as 3. If I change the parameter to varchar2, then the output will come as 2 even without changing the procedure body.
Re: Spliting of String [message #632816 is a reply to message #632813] Fri, 06 February 2015 00:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Go on.

Re: Spliting of String [message #632828 is a reply to message #632816] Fri, 06 February 2015 02:50 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

There are two test cases with results.How it differs .
Declare
--Lv_Str Clob:='99~';
Lv_Str Varchar2(100):='99~';
Pos number:=0;
Begin   
 Dbms_Output.Put_Line('length:'||Length(lv_str));
 Lv_Str := Substr(Lv_Str, 3 + 1, Length(Lv_Str));
  Dbms_Output.Put_Line('str:'||Lv_Str||'vcvc');
 Pos := Instr(Lv_Str, '~', 1, 1);
dbms_output.put_line('pos:'||Pos);
end;

--output
length:3
str:vcvc
pos:



Declare
Lv_Str Clob:='99~';
--Lv_Str Varchar2(100):='99~';
Pos number:=0;
Begin   
 Dbms_Output.Put_Line('length:'||Length(Lv_Str));
 Lv_Str :=Substr(Lv_Str, 3 + 1, Length(Lv_Str));
  Dbms_Output.Put_Line('str:'||Lv_Str||'vcvc');
 Pos := Instr(Lv_Str, '~', 1, 1);
dbms_output.put_line('pos:'||Pos);
End;
--output
length:3
Str:Vcvc
pos:0

[Updated on: Fri, 06 February 2015 02:51]

Report message to a moderator

Re: Spliting of String [message #632830 is a reply to message #632828] Fri, 06 February 2015 02:55 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
1) It doesn't differ in those examples (ignoring the fact that you've somehow upper cased part of the result in the 2nd case - we tell people to copy and paste the output for a reason)
2) I have no idea what you think those examples are supposed to prove.
Re: Spliting of String [message #632832 is a reply to message #632830] Fri, 06 February 2015 03:00 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks CookieMonster,

There is differnece of output of both the blocks one is returning pos:0 with clob as parameter ,but pos: (null) with varchar2 as parameter.
Can you explain why this occured.
Re: Spliting of String [message #632838 is a reply to message #632832] Fri, 06 February 2015 03:23 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
SQL> Declare
  2  Lv_Str Clob:='99~';
  3  --Lv_Str Varchar2(100):='99~';
  4  Pos number:=0;
  5  Begin
  6   Dbms_Output.Put_Line('length:'||Length(Lv_Str));
  7   Lv_Str :=Substr(Lv_Str, 3 + 1, Length(Lv_Str));
  8    Dbms_Output.Put_Line('str:'||Lv_Str||'vcvc');
  9    Dbms_Output.Put_Line('length after substr:'||Length(Lv_Str));
 10     Pos := Instr(Lv_Str, '~', 1, 1);
 11     dbms_output.put_line('pos after substr:'||Pos);
 12    lv_str := NULL;
 13    Dbms_Output.Put_Line('length after set null:'||Length(Lv_Str));
 14   Pos := Instr(Lv_Str, '~', 1, 1);
 15  dbms_output.put_line('pos after set null:'||Pos);
 16  End;
 17  /
 
length:3
str:vcvc
length after substr:0
pos after substr:0
length after set null:
pos after set null:
 
PL/SQL procedure successfully completed
 
SQL> 


It seems that the clob isn't set to null after the substr while the varchar is.
instr of null is always null regardless of what parameters 2-4 are set to.
Re: Spliting of String [message #632844 is a reply to message #632838] Fri, 06 February 2015 03:58 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

That may be correct,but it is not visible in output.
Re: Spliting of String [message #632883 is a reply to message #632844] Fri, 06 February 2015 10:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What prevent you from making some test to know the answer?

Re: Spliting of String [message #632885 is a reply to message #632883] Fri, 06 February 2015 10:05 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>What prevent you from making some test to know the answer?
incompetence?
Lazy?

take your pick.
Re: Spliting of String [message #633052 is a reply to message #632885] Tue, 10 February 2015 08:32 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member


My confusion is,AT line 7 what IS THE exact VALUE OF Lv_Str.
1.Definitely it IS NOT NULL because LENGTH IS 0 AT line 9.
2.Definitely it IS NULL because OF THE output (str:vcvc)--nothing was printed between
Re: Spliting of String [message #633053 is a reply to message #633052] Tue, 10 February 2015 08:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>nothing was printed between
nothing VISIBLE was printed between

did you ever hear about "control characters" where ASCII(value) is less than 32 (decimal)?

You can make the content visible by use ASCIISTR() function
Re: Spliting of String [message #633056 is a reply to message #633053] Tue, 10 February 2015 09:06 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank You BlackSwan,

I have added these two lines after line 7 but still nothing is appearing.
Dbms_Output.Put_Line( ASCIISTR(Lv_Str) );
Dbms_Output.Put_Line( ASCII(Lv_Str) );
Re: Spliting of String [message #633057 is a reply to message #633056] Tue, 10 February 2015 09:09 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
do NOT tell us!
use COPY & PASTE to SHOW us what exactly you have & how Oracle responded!
Re: Spliting of String [message #633058 is a reply to message #633057] Tue, 10 February 2015 09:19 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

The output I got as follows,
 Declare
    Lv_Str Clob:='99~';
    --Lv_Str Varchar2(100):='99~';
    Pos number:=0;
    Begin
     Dbms_Output.Put_Line('length:'||Length(Lv_Str));
     Lv_Str :=Substr(Lv_Str, 3 + 1, LENGTH(Lv_Str));
      Dbms_Output.Put_Line( ASCIISTR(Lv_Str) );
       Dbms_Output.Put_Line( ASCII(Lv_Str) );
      Dbms_Output.Put_Line('str:'||ASCIISTR(Lv_Str)||'vcvc');
      Dbms_Output.Put_Line('length after substr:'||Length(Lv_Str));
      Pos := Instr(Lv_Str, '~', 1, 1);
      dbms_output.put_line('pos after substr:'||Pos);
     lv_str := NULL;
     Dbms_Output.Put_Line('length after set null:'||Length(Lv_Str));
    Pos := Instr(Lv_Str, '~', 1, 1);
   dbms_output.put_line('pos after set null:'||Pos);
   End;
 
 --output
length:3


str:vcvc
length after substr:0
pos after substr:0
length after set null:
pos after set null:

Re: Spliting of String [message #633061 is a reply to message #633058] Tue, 10 February 2015 09:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
What do you learn from below?
  1   Declare
  2      Lv_Str Clob:='99~';
  3      --Lv_Str Varchar2(100):='99~';
  4      Pos number:=0;
  5      Begin
  6       Dbms_Output.Put_Line('length1:'||Length(Lv_Str));
  7       Lv_Str :=Substr(Lv_Str, 3 + 1, LENGTH(Lv_Str));
  8       Dbms_Output.Put_Line('length2:'||Length(Lv_Str));
  9        Dbms_Output.Put_Line( ASCIISTR(Lv_Str) );
 10         Dbms_Output.Put_Line( ASCII(Lv_Str) );
 11        Dbms_Output.Put_Line('str:'||ASCIISTR(Lv_Str)||'vcvc');
 12        Dbms_Output.Put_Line('length after substr:'||Length(Lv_Str));
 13        Pos := Instr(Lv_Str, '~', 1, 1);
 14        dbms_output.put_line('pos after substr:'||Pos);
 15       lv_str := NULL;
 16       Dbms_Output.Put_Line('length after set null:'||Length(Lv_Str));
 17      Pos := Instr(Lv_Str, '~', 1, 1);
 18     dbms_output.put_line('pos after set null:'||Pos);
 19*    End;
SQL> /
length1:3
length2:0
str:vcvc
length after substr:0
pos after substr:0
length after set null:
pos after set null:

PL/SQL procedure successfully completed.

SQL> 


Re: Spliting of String [message #633065 is a reply to message #633061] Tue, 10 February 2015 09:35 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Well unless I'm missing something I'm learning that neither ASCIISTR nor ASCII will tell us what's going on here.
Re: Spliting of String [message #633067 is a reply to message #633065] Tue, 10 February 2015 09:38 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
cookiemonster wrote on Tue, 10 February 2015 07:35
Well unless I'm missing something I'm learning that neither ASCIISTR nor ASCII will tell us what's going on here.


This frequently occurs when trying to debug code that you can not actually see.
Re: Spliting of String [message #633069 is a reply to message #633067] Tue, 10 February 2015 09:43 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
??????
I can see code.
I can run the code.
I don't understand what it's doing.
Try being useful instead of cryptic.
Re: Spliting of String [message #633071 is a reply to message #633069] Tue, 10 February 2015 09:46 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
It is a challenge to split a string of ZERO length!
Re: Spliting of String [message #633073 is a reply to message #633071] Tue, 10 February 2015 09:59 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Not as a big a challenge as getting you to be helpful obviously.
Last I checked, in oracle, zero length string = null.
That's apparently not the case here.
I'd like to know why.
Re: Spliting of String [message #633074 is a reply to message #633073] Tue, 10 February 2015 10:10 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
@sss111ind
Read this: http://docs.oracle.com/cd/E11882_01/appdev.112/e18294/adlob_working.htm#ADLOB002
Re: Spliting of String [message #633077 is a reply to message #633074] Tue, 10 February 2015 10:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Also http://www.orafaq.com/forum/m/627385/?srch=LOB+length#msg_627385

Re: Spliting of String [message #633113 is a reply to message #633077] Wed, 11 February 2015 02:50 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thank You very much CookieMonster,Michel,BlackSwan.
Re: Spliting of String [message #633134 is a reply to message #633113] Wed, 11 February 2015 07:36 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
If you found an answer yourself, post it. That way we know the issue is resolved and we might learn from it.
Previous Topic: how do I produce columns based on values within a column
Next Topic: SQL output to HTML table in email notification - possible?
Goto Forum:
  


Current Time: Thu Apr 25 15:07:03 CDT 2024