Home » SQL & PL/SQL » SQL & PL/SQL » Issue with Number Conversion !! (Oracle 11.2.0.1, RHEL)
Issue with Number Conversion !! [message #639344] Mon, 06 July 2015 07:10 Go to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Hello,

I am working on a small enhancement to an existing code, where the requirement is as follows:

1. User of a PL-SQL procedure enters a value like: Album:8|Application:25|Game:25|Music:8|Track:8
2. The task is to calculate the sum of the numbers above.

Here is a small piece of code I am working on to achieve this on run time.
This will later be incorporated into the final code, once it is ready.

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  TYPE content IS TABLE OF VARCHAR2(50)
    INDEX BY BINARY_INTEGER;
  
  v_tab  content;
  v_idx  NUMBER;
  counter NUMBER := 1;
  -- v_content VARCHAR2(50);
  v_content_value VARCHAR2(50);
  v_total NUMBER;
BEGIN
  -- Initialise the collection.
  << load_loop >>
  FOR i IN (select replace('Album:8|Application:25|Game:25|Music:8|Track:8','|',chr(10)) as content_text from dual) LOOP
    v_tab(counter) := i.content_text;
  END LOOP load_loop;
  
  -- Traverse sparse collection
  v_idx := v_tab.FIRST;
  << display_loop >>
  WHILE v_idx IS NOT NULL LOOP
    DBMS_OUTPUT.PUT_LINE('The content: ' || v_tab(v_idx));
    -- v_content := v_tab(v_idx);
    select replace(replace(replace(replace(replace(v_tab(v_idx),'Album:',''),'Application:',''),'Game:',''),'Music:',''),'Track:','')
    as content_value_number
    into v_content_value
    from dual;
    DBMS_OUTPUT.PUT_LINE('The content value: ' || chr(10) ||  v_content_value);
    v_total := v_total + to_number(v_content_value);
    DBMS_OUTPUT.PUT_LINE('The content value in number : ' || chr(10) || v_total);
    v_idx := v_tab.NEXT(v_idx);
  END LOOP display_loop;
END;
/


When I run this code, I am getting the error as:

ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 29
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    An arithmetic, numeric, string, conversion, or constraint error
           occurred. For example, this error occurs if an attempt is made to
           assign the value NULL to a variable declared NOT NULL, or if an
           attempt is made to assign an integer larger than 99 to a variable
           declared NUMBER(2).
*Action:   Change the data, how it is manipulated, or how it is declared so
           that values do not violate constraints.


I have also tried using a SQL Query (SELECT) to cast the value as number and then print it.
Example:

    select cast(v_content_value as INTEGER)  into v_total
    from dual;


Even this does not work.
I have even tried using TO_NUMBER(TRIM()) functions, but that does not work as well!

Any suggestions to achieve this will be really appreciated!

Thanks
Suddhasatwa
Re: Issue with Number Conversion !! [message #639359 is a reply to message #639344] Mon, 06 July 2015 07:54 Go to previous messageGo to next message
JNagtzaam
Messages: 36
Registered: July 2015
Location: Alkmaar
Member

You have to re-think this. Replacing '|' with chr(10) does NOT create a table. You still have just 1 row in v_tab.
You need to convert the delimited string to a 'table'. There are diffent ways to do this. Have a look at:http://www.oracle-developer.net/display.php?id=412.
Re: Issue with Number Conversion !! [message #639366 is a reply to message #639344] Mon, 06 July 2015 08:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
1. User of a PL-SQL procedure enters a value like: Album:8|Application:25|Game:25|Music:8|Track:8
2. The task is to calculate the sum of the numbers above.


SQL> with data as (select 'Album:8|Application:25|Game:25|Music:8|Track:8' val from dual)
  2  select xmlquery(regexp_replace(val,'[^0-9]+','+') returning content).getNumberVal() total
  3  from data
  4  /
     TOTAL
----------
        74

[Updated on: Mon, 06 July 2015 08:26]

Report message to a moderator

Re: Issue with Number Conversion !! [message #639994 is a reply to message #639366] Tue, 21 July 2015 03:56 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Many thanks to all, the issue is now resolved.
Re: Issue with Number Conversion !! [message #639995 is a reply to message #639994] Tue, 21 July 2015 03:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

And what is your final solution?

Re: Issue with Number Conversion !! [message #639997 is a reply to message #639995] Tue, 21 July 2015 04:02 Go to previous message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Hi Michael

I used your solution - the SQL query you gave above, and it is working perfectly for all the possible input combinations I have.

many thanks, again.
Suddhasatwa
Previous Topic: Bulk collect limit on what basis we will keep
Next Topic: Creating SQL String
Goto Forum:
  


Current Time: Thu Apr 18 09:23:33 CDT 2024