Created
November 19, 2021 16:50
-
-
Save pweinzettel/574d06de6a88ecc590193c767ff3fce9 to your computer and use it in GitHub Desktop.
bytes2human
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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