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 -> Re: Decimal to Octal conversion function in (PL/)SQL

Re: Decimal to Octal conversion function in (PL/)SQL

From: James Lorenzen <lorenzen_at_tiny.net>
Date: Tue, 16 Jun 1998 14:50:44 GMT
Message-ID: <lorenzen-ya02408000R1606980950410001@news.visi.com>


In article <35816056.0_at_gatekeeper.umassd.edu>, dhassel_at_umassd.edu wrote:

>Does anyone have a function that converts a decimal value into octal for
>SQL*Plus or PL/SQL?
>DHassel_at_UMassD.Edu

This will convert to any base from 2 through 16. Very little error checking. It will return a VARCHAR2 string.
HTH
   James

CREATE OR REPLACE FUNCTION NBR_CONV
   (ipt_nbr IN NUMBER, ipt_base IN NUMBER := 16)    RETURN VARCHAR2
   IS

     base_str varchar2(255) := NULL ;
     hex_str  VARCHAR2(37) := '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ?' ;
     w_nbr    NUMBER(20) ;
     nbr_base NUMBER := 16 ;
  BEGIN
     IF ipt_base < 2 OR ipt_base > 16 
     THEN
        nbr_base := 16 ;
     ELSE
        nbr_base := ipt_base ;
     END IF ;
     base_str := NULL ;
     w_nbr := ipt_nbr ;
     LOOP
     EXIT WHEN w_nbr = 0 ;
        base_str := SUBSTR(hex_str,to_char(MOD(w_nbr,nbr_base))+1,1)
           || base_str ;
        w_nbr := TRUNC(w_nbr / nbr_base,0) ;
     END LOOP ;
     RETURN base_str ;

  END NBR_CONV ;
/
--
lorenzen_at_tiny.net             | Life is complex; it has
                              |   real and imaginary parts
Received on Tue Jun 16 1998 - 09:50:44 CDT

Original text of this message

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