Octal

From Oracle FAQ
Jump to: navigation, search

Octal is a numbering system with 8 digits: 0 to 7.

[edit] Conversion functions

Use the following conversion PL/SQL functions to convert octal values to decimal and vice versa:

CREATE OR REPLACE FUNCTION oct2dec (octval in char) RETURN number IS
  i                 number;
  digits            number;
  result            number := 0;
  current_digit     char(1);
  current_digit_dec number;
BEGIN
  if octval is null then
    return null;
  end if;
  digits := length(octval);
  for i in 1..digits loop
     current_digit := SUBSTR(octval, i, 1);
     current_digit_dec := to_number(current_digit);
     result := (result * 8) + current_digit_dec;
  end loop;
  return result;
END oct2dec;
/
 
SQL> SELECT oct2dec(127662) FROM dual;
OCT2DEC(127662)
---------------
          44978
CREATE OR REPLACE FUNCTION dec2oct (N in number) RETURN varchar2 IS
  octval varchar2(64);
  N2     number := N;
BEGIN
  if N is null then
    return null;
  end if;
  while ( N2 > 0 ) loop
     octval := mod(N2, 8) || octval;
     N2 := trunc( N2 / 8 );
  end loop;
  return octval;
END dec2oct;
/

SQL> SELECT dec2oct(44978) FROM dual;
DEC2OCT(44978)
--------------
127662

[edit] Also see

  • Binary - numbering system with 2 digits
  • Hexadecimal - numbering system with 16 digits
Glossary of Terms
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #