Spliting of String [message #632769] |
Thu, 05 February 2015 09:29 |
|
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 #632828 is a reply to message #632816] |
Fri, 06 February 2015 02:50 |
|
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 |
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 #632838 is a reply to message #632832] |
Fri, 06 February 2015 03:23 |
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 #633058 is a reply to message #633057] |
Tue, 10 February 2015 09:19 |
|
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 #633073 is a reply to message #633071] |
Tue, 10 February 2015 09:59 |
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.
|
|
|
|
|
|
|