Oracle - Number to varchar - oracle

I have a table containing a column of type Number
create table tmp (
/*other fields*/
some_field Number
)
and in a PL SQL script, I want to convert that field to a varchar. However, i don't know its length, so I get an exception
Exception message is ORA-06502:
PL/SQL: numeric or value error:
character string buffer too small
v_some_field varchar(21);
/*...*/
v_some_field := TO_CHAR(some_field,'999999999999999999999');
How should i declare the v_some_field buffer? Setting it to varchar(32767) seems quite brute, is there any alternative?

you're getting an error not because the number is too large but because the result of your to_char is 22 characters long (21x"9"+one character for the sign):
SQL> DECLARE
2 some_field NUMBER := 123;
3 v_some_field VARCHAR(21);
4 BEGIN
5 v_some_field := TO_CHAR(some_field, '999999999999999999999');
6 END;
7 /
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 6
SQL> DECLARE
2 some_field NUMBER := 123;
3 v_some_field VARCHAR(22);
4 BEGIN
5 v_some_field := TO_CHAR(some_field, '999999999999999999999');
6 END;
7 /
PL/SQL procedure successfully completed

You could determine the maximum length of your converted varchar2 by converting a negative value with integral and fractional digits:
set serveroutput on
declare
n number;
begin
n := -4/3;
dbms_output.put_line(length(to_char(n)));
end;
/
Output is 41 for me.

Related

How to solve : ORA-06502: PL/SQL: numeric or value error?

I have a long text in CLOB and I'm trying to use the function htp.prn
Here is my code :
n := dbms_lob.getlength(html); -- n is 31745 characters
if n<32000 then
htp.prn(HTML); -- << Error raised here
else
do something else
end if;
When using the function htp.prn, I'm getting the error : ORA-06502: PL/SQL: numeric or value error
I know he text size allowed is 32672 characters. And the text contains some chinese characters. So, I assume the problem is coming from dbms_lob.getlength
Does anyone know how to solve that please ?
Thanks
Cheers,
If you have Unicode characters (you mentioned Chinese), and your database is using a multibyte characterset, then the number of bytes is greater than the number of characters. 32767 is a hard limit of bytes. Whereas, dbms_lob.getlength for CLOBs and NCLOBs counts characters. So you probably are over the 32K limit in bytes.
You cannot easily get the length of a CLOB in bytes. There are various unpleasant workarounds, but probably the easiest thing for you to do is simply trap the exception rather than predict it:
n := dbms_lob.getlength(html);
if n<32000 then
declare
value_error exception;
pragma exception_init(value_error,-06502);
begin
htp.prn(HTML);
exception
when value_error then
null;
end;
else
do something else
end if;
Multibyte characters, eh?
Perhaps you could handle it by splitting that string into smaller chunks.
My example uses not that long string which is then split into 20 characters in length. I'm just displaying them (line #10); you'd htp.prn it (line #11).
SQL> set serveroutput on
SQL> declare
2 html clob := 'How to solve: ORA-06502: PL/SQL: numeric or value error';
3 l_var varchar2(20); --> you'd use e.g. 5000
4 l_size number := 20; --> you'd use e.g. 5000
5 l_pos number := 1;
6 begin
7 while dbms_lob.getlength(html) >= l_pos
8 loop
9 l_var := dbms_lob.substr(html, l_size, l_pos);
10 dbms_output.put_line(l_var);
11 htp.prn(l_var);
12 l_pos := l_pos + length(l_var);
13 end loop;
14 end;
15 /
How to solve: ORA-06
502: PL/SQL: numeric
or value error
PL/SQL procedure successfully completed.
SQL>

ORA-06502 with ORA-06512

I have a procedure in which I'm trying to write a source code (1290 lines) to dbms_output like this:
dbms_output.put_line(DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner')); --MYPROC, line 6
I'm getting :
ORA-06502: PL/SQL: numeric or value error
ORA-06512: in "MYPROC", line 6
.
This error occures in toad.
I can execute in editor tab of toad:
SELECT DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') FROM DUAL;
I mean I'm getting the source code in 'Data grid'.
Same happens when I try to store the code in a CLOB variable:
src CLOB;
...
src := DBMS_METADATA.GET_DDL('FUNCTION', 'name', 'owner') ; --MYPROC, line 6
Any clue?
From the documentation for dbms_output:
The maximum line size is 32767 bytes.
That means that you can't pass more than that in a single put_line call. You are currently passing your whole CLOB, which at 1290 lines is likely to exceed that limit. And the error you get when you do that is "ORA-06502: PL/SQL: numeric or value error", as you are seeing.
You can split your CLOB into smaller chunks, and as it is already multiple lines it makes sense to make each chunk a single line from the DDL. You can do that by looking for newline characters, extracting all the text up to the next one, and printing that. You need a few variables to keep track of where you are. Something like this should work for you:
declare
src clob;
src_length pls_integer;
pos pls_integer := 1;
buffer varchar2(32767);
amount pls_integer := 32767;
begin
src := dbms_metadata.get_ddl('FUNCTION', 'TEST_FUNCTION_1', user);
src_length := dbms_lob.getlength(src);
while pos < src_length loop
-- read to next newline if there is one, rest of CLOB if not
if dbms_lob.instr(src, chr(10), pos) > 0 then
-- see how many charcaters there are until next newline
amount := dbms_lob.instr(src, chr(10), pos) - pos;
-- if there are any, read them into the buffer; otherwise clear it
if amount > 0 then
dbms_lob.read(src, amount, pos, buffer);
else
buffer := null;
end if;
pos := pos + amount + 1; -- skip newline character
else
-- no newline so read everything that is left
amount := 32767;
dbms_lob.read(src, amount, pos, buffer);
pos := pos + amount;
end if;
dbms_output.put_line(buffer);
end loop;
end;
/
It won't work if you have a single line (without or without a newline at the end) that is more than 32k, which hopefully won't be an issue with DDL. (You could sort of handle it, but doing so would inject additional newlines, which wouldn't be good either.)
What you are saying can't be true. DBMS_OUTPUT.PUT_LINE can't be used at SQL level, it belongs to PL/SQL.
What is MYPROC and what does it contain at line #6?
Which "editor" is "I can execute in editor"?
Numeric or value error is usually related to the fact that you're trying to store "large" values into a "small" variable:
SQL> declare
2 l_var varchar2(2);
3 begin
4 l_var := 'ABC';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
SQL>
which is what - I presume - you did.
Another cause is wrongly declared variable, e.g.
SQL> declare
2 l_var number;
3 begin
4 l_var := 'A';
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
SQL>
I'll try to guess what you might be doing:
SQL> set serveroutput on
SQL> DECLARE
2 src CLOB;
3 BEGIN
4 src := DBMS_METADATA.GET_DDL ('PACKAGE', 'MY_PKG', 'SCOTT');
5 DBMS_OUTPUT.put_line ('len = ' || DBMS_LOB.getlength (src));
6 END;
7 /
len = 67239
PL/SQL procedure successfully completed.
SQL>
As you can see, it works OK for me. Package isn't that small (see its length), so - can't really tell what you did wrong. I'd suggest you to do exactly as I did - copy/paste code I posted above (those 7 lines), fix information (function, its name, owner) and post the result by editing the original question, not as a comment.

PLS-00215: String length constraints must be in range (1 .. 32767)

I have a oracle package and there is a function in it which is having out parameters.
the package name lets say ppp and function name lets say fff.
function is below and i am unable to execute the function with out parameters.
function-
FUNCTION fff (P_FID NUMBER,DUCTBANKLABEL VARCHAR2, SERVICEDUCTVALID OUT NUMBER ) RETURN VARCHAR2
----End of R3 - Obs#195 (1 Oct 2018)
AS
CNT NUMBER;
INSTDATE VARCHAR2(100);
DUCTSIZE NUMBER;
.
.
.
.
many more.......;
BEGIN
.
.
.function does its thing and returns the value
END;
Now am trying to call this function with a pl/sql block like this-
set serveroutput on;
declare
aa NUMBER:=129685933;
bb VARCHAR2:='1705297E TO P5547635';
cc NUMBER;
ANS VARCHAR2;
BEGIN
ANS:=ppp.fff(aa,bb,cc);
DBMS_OUTPUT.put_line(ANS);
END;
But getting the below error-
Error report:
ORA-06550: line 3, column 4:
PLS-00215: String length constraints must be in range (1 .. 32767)
ORA-06550: line 5, column 5:
PLS-00215: String length constraints must be in range (1 .. 32767)
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Please help on how can i execute this function
You need to specify VARCHAR2 variable lengths in your anonymous PL/SQL block.
DECLARE
aa NUMBER := 129685933;
bb VARCHAR2 (30) := '1705297E TO P5547635';
cc NUMBER;
ANS VARCHAR2 (200);
BEGIN
ANS := ppp.fff (aa, bb, cc);
DBMS_OUTPUT.put_line (ANS);
END;
varchar2 variables need to have a length as the error indicates.
Line 3
bb VARCHAR2:='1705297E TO P5547635';
and line 5
ANS VARCHAR2;
are both missing lengths. Assuming you want both to allow, say, 100 characters
bb VARCHAR2(100):='1705297E TO P5547635';
ANS VARCHAR2(100);

To_CHAR result cannot go in VARCHAR2

There is somthing strange I wouls like to understand
let's say we have this code :
DECLARE
a varchar2(6);
BEGIN
a := '000001';
END;
This works good. No problem
Now we have this :
DECLARE
a varchar2(6);
BEGIN
a := TO_CHAR(1, '000000');
END;
It does not work:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
why?
A problem with nls_language maybe? what problem? what solution?
Thank you
The TO_CHAR result has a space at the beginning, making its total length 7 characters. The space is reserved for a minus sign if the number is negative. If you try this query you'll see:
SELECT '[' || TO_CHAR(1, '000000') || ']' FROM DUAL;
The result is:
[ 000001]
^ space for a minus sign
To get the result without the space, use the FM modifier:
a := TO_CHAR(1, 'FM000000');
Or, just use `LTRIM' :
SQL> set serveroutput on;
SQL> DECLARE
2 a VARCHAR2(6);
3 BEGIN
4 A := LTRIM(TO_CHAR(1, '000000'));
5 dbms_output.put_line(a);
6 END;
7 /
000001
PL/SQL procedure successfully completed.
SQL>
Note that a leading blank only appears before a positive number with no sign formatting. No leading blank appears before a negative number, or before any signed number, regardless of the placement of the sign.
The “FM” format to override the default leading blank for unsigned positive numbers

MD5 in Oracle (DBMS_OBFUSCATION_TOOLKIT.MD5)

I'm trying to compose a function to obtain MD5 hashes from bits I've gathered here and there. I want to obtain the lower-case hexadecimal representation of the hash. I have this so far:
CREATE OR REPLACE FUNCTION MD5 (
CADENA IN VARCHAR2
) RETURN DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM
AS
BEGIN
RETURN LOWER(
RAWTOHEX(
UTL_RAW.CAST_TO_RAW(
DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => CADENA)
)
)
);
END;
I'm not sure about the return type of the function. DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM looks like the appropriate choice and as far as I can tell it works as expected but the package definition for dbms_obfuscation_toolkit as displayed by SQL Developer shows this:
SUBTYPE varchar2_checksum IS VARCHAR2(16);
The output has 32 characters so I must be doing something wrong. My questions:
What's the correct type for the RETURN statement?
Am I doing unnecessary conversions to calculate the hash?
Here you go:
create or replace function getMD5(
in_string in varchar2)
return varchar2
as
cln_md5raw raw(2000);
out_raw raw(16);
begin
cln_md5raw := utl_raw.cast_to_raw(in_string);
dbms_obfuscation_toolkit.md5(input=>cln_md5raw,checksum=>out_raw);
-- return hex version (32 length)
return rawtohex(out_raw);
end;
The 32 length is because it is a hex representation of the raw(16) value. Or, modify above to output the raw version and store the raw in a RAW column (less space used, but you'll be doing future rawtohex and hextoraw conversions, believe me).
Cheers
It's a peculiarity of Oracle PL/SQL that stored procedure parameters and function return types cannot be limited. That is, we cannot have a procedure with a signature like this:
SQL> create or replace procedure my_proc (p1 in varchar2(30))
2 is
3 begin
4 null;
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> show error
Errors for PROCEDURE MY_PROC:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/34 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue.
SQL> create or replace procedure my_proc (p1 in varchar2)
2 is
3 begin
4 null;
5 end;
6 /
Procedure created.
SQL>
Sure we can define the procedure's parameter using a SUBTYPE but Oracle will ignore it. Same goes for function return types...
SQL> create or replace package my_subtypes as
2 subtype ltd_string is varchar2(30);
3 end;
4 /
Package created.
SQL> create or replace function my_func return my_subtypes.ltd_string
2 is
3 begin
4 return lpad('a', 4000, 'a');
5 end;
6 /
Function created.
SQL> select length(my_func) from dual
2 /
LENGTH(MY_FUNC)
---------------
4000
SQL>
The only way of limiting parameters and return types is to declare variables using subtypes within the stored procedure. Use the variables within the package, and assign them to the OUT paramters (or RETURN the variable for functions).
Which is a long-winded way of saying, you can use DBMS_OBFUSCATION_TOOLKIT.VARCHAR2_CHECKSUM in your code confident that it won't prevent your function returning 32 characters.
However, it will confuse developers who will lookup the SUBTYPE declaration. In the worst case these people will use the subtype to declare their own working variables with the following tragic result:
SQL> declare
2 v my_subtypes.ltd_string;
3 begin
4 v := my_func;
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
SQL>
So, it is better not to use an inappropriate subtype. Instead declare your own.

Resources