Concatenation with Zero is not occurring properly in code?

Posted by Vineet on Stack Overflow See other posts from Stack Overflow or by Vineet
Published on 2010-04-04T14:58:57Z Indexed on 2010/04/04 15:43 UTC
Read the original article Hit count: 379

Filed under:
|
|

I was trying to reverse a number in PL/SQL. It's working fine, but when my number contains any 0, the output is unexpected. For example:

1234 output 4321
1000 output 1
1203 ouput 3021
10001 output 1


DECLARE
r number(9);
num number(9):=&p_num;
BEGIN
    WHILE num>=1 LOOP 
        IF mod(num,10)=0 THEN -- extracting last digit of a number .
            r:=r||0; --if end digit is 0 then concat r with 0
        ELSE
            r:=r||mod(num,10);--if mod is not 0 then it would be last digit.
        END IF; 
            num:=trunc(num/10);--Removing last digit from number
    END LOOP;
dbms_output.put_line(r);
END;

© Stack Overflow or respective owner

Related posts about Oracle

Related posts about plsql