Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> need help with simple Function

need help with simple Function

From: Randy Harris <randy_at_SpamFree.com>
Date: Wed, 12 Jan 2005 07:17:41 GMT
Message-ID: <pa4Fd.8392$Vj3.7618@newssvr17.news.prodigy.com>


I'm new to PL/SQL, need help with what should be (I think) a simple problem. I'm trying to sum the digits in a number, here's what I have so far:

CREATE OR REPLACE FUNCTION Session_Hash

        (sessid IN NUMBER ) RETURN NUMBER     IS

        dig_sum INTEGER;
        sesschar VARCHAR2(8);

BEGIN
    sesschar := TO_CHAR(sessid) ;
    FOR i IN 1..LENGTH(sesschar) LOOP

        dig_sum := dig_sum + TO_NUMBER(SUBSTR(sesschar,i,1));     END LOOP;
    RETURN(dig_sum);
END;
/

This way it ends up returning nothing. If I change the line inside the loop to:

        dig_sum := TO_NUMBER(SUBSTR(sesschar,i,1));

it returns the last digit in the number (so I know it is getting the number), but I need it to sum all of the digits.

Please tell me what I am doing wrong. Received on Wed Jan 12 2005 - 01:17:41 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US