Skip to content

Instantly share code, notes, and snippets.

@pweinzettel
Created November 19, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pweinzettel/574d06de6a88ecc590193c767ff3fce9 to your computer and use it in GitHub Desktop.
Save pweinzettel/574d06de6a88ecc590193c767ff3fce9 to your computer and use it in GitHub Desktop.
bytes2human
create or replace function bytes2human(val in varchar2) return varchar2 is
res varchar2(50);
num number;
unit varchar2(1);
pow integer;
type array_t is varray(6) of varchar2(1);
pow2unit array_t := array_t('', 'K', 'M', 'G', 'T', 'P');
begin
num := regexp_replace(val, '[^0-9,.]', '');
unit := upper(regexp_replace(val, '[^a-z and ^A-Z]', ''));
select decode(unit, '', 0, 'K', 1, 'M', 2, 'G', 3, 'T', 4, 'P', 5, 'ERR') into pow from dual;
if pow > 0 then
res := num * power(1024,pow);
else
while num >= 1024 and pow < 5 loop
num := num/1024;
pow := pow+1;
end loop;
res := trunc(num,2) || pow2unit(pow+1);
end if;
return(res);
end bytes2human;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment