Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: need help with simple Function
Randy Harris wrote:
> 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.
You can not add a value to NULL. Rewrite as:
dig_sum INTEGER := 0;
-- Daniel A. Morgan University of Washington damorgan_at_x.washington.edu (replace 'x' with 'u' to respond) ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups ---= East/West-Coast Server Farms - Total Privacy via Encryption =---Received on Wed Jan 12 2005 - 10:31:33 CST
![]() |
![]() |