Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with TO_CHAR

Status
Not open for further replies.

weberm

Programmer
Dec 23, 2002
240
US
I am attempting to increment a counter field which is stored as a CHAR(4) using a NUMBER(4) variable and the TO_CHAR function but keep getting a VALUE ERROR exception:

Code:
 v_detail_line := v_detail_line +1;
 CCRR_rec.fee_detail_line_number := TO_CHAR(v_detail_line,'0009');

However, using LPAD works just fine:
Code:
v_detail_line := v_detail_line +1;
CCRR_rec.fee_detail_line_number := LPAD(v_detail_line,4,'0');

Does anyone know what's going on here? I'm curious but am probably overlooking something.

 
The two functions produce similar results: the "to_char" function produces an "extra" left-side character position for a '-' in the case of a negative resulting value; the "lpad" function does not produce the extra sign position.

I cannot, however, explain (without more information), why you are receiving the "value error" exception. Can you please introduce several "dbms_output.put_line" commands amongst your existing code to confirm the "before" and "after" values of "v_detail_line"?

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
If TO_CHAR creates an extra character then that cerainly would explain why I got a VALUE_ERROR exception! However, none of the values were negative.

In any event, I was running it in debug mode in TOAD and v_detail_line = 2 when it invokes TO_CHAR. Weird,huh?

Would it help if I mentioned CCRR_rec.fee_detail_line_number was a record declared as a ROWTYPE of a table named CCRR with field fee_detail_line_number as CHAR(4)? I tried declaring v_detail_line as NUMBER and NUMBER(4) without any success.
 

-- Or --

Use this mask:
Code:
CCRR_rec.fee_detail_line_number := TO_CHAR(v_detail_line,'FM0009');
[3eyes]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Amazing how full disclosure can solve a problem <grin>...the problem with "fee_detail_line_number as CHAR(4)" is that it is not large enough to accommodate your 4-character mask plus the sign byte:
Code:
declare
    x varchar2(4);
begin
    x := to_char(4,'0009');
end;
/

ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4
Let us know if this resolves your issue.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 

Exactly:
Code:
SQL> declare
  2      x varchar2(4);
  3  begin
  4      x := to_char(4,'FM0009');
  5* end;
SQL>/

PL/SQL procedure successfully completed.

[thumbsup2]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top