Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

djberg96 (2603)

djberg96
  (email not shown publicly)

Journal of djberg96 (2603)

Wednesday August 04, 2004
03:34 PM

to_dec for Oracle

[ #20235 ]
We had some data that was, for reasons I dare not ask, in hex instead of decimal. Surprisingly there is no Oracle SQL function to convert numbers of one base to another (that I saw - if there is, please let me know). The convert() function in Sybase can do this, but the Oracle version works differently.

Anyway I came across this:

create or replace function to_dec
( p_str in varchar2,
  p_from_base in number default 16 ) return number
is
    l_num   number default 0;
    l_hex   varchar2(16) default '0123456789ABCDEF';
begin
    if ( p_str is null or p_from_base is null )
    then
        return null;
    end if;
    for i in 1 .. length(p_str) loop
        l_num := l_num * p_from_base + instr(l_hex,upper(substr(p_str,i,1)))-1;
    end loop;
    return l_num;
end to_dec;
/

Works like a charm.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.