Wednesday, June 22, 2011

Oracle PL/SQL Function - Hybrid Kibibyte/Engineering Number Presentation

Create or Replace Function KiBEng
/* version 0.5 # Hybrid Kibibyte/Engineering Number Presentation # Jeff I # 2011-MAY-10 */
( bar IN Number )
Return VarChar2
IS
strout VarChar2(16);
Begin
If bar < 0 Then Raise_Application_Error(-20001,'Number Is Less Than Zero');
End If;
If bar is Null Then Raise_Application_Error(-20002,'Number Is NULL');
End If;
If bar = 0 Then strout := '0_B';
Else Select
To_Char(bar/Power(1024,Floor(Log(1024,bar))),'999.999') ||
Decode(Floor(Log(1024,bar)),0,'_B',1,'_Kib',2,'_MiB',3,'_GiB',4,'_TiB',4,'_PiB','-ERR') Into strout
From DUAL;
End If;
Return strout;
Exception
When Others Then Raise_Application_Error(-20000,'That Ain`t Right! '||SQLCODE||' - '||SQLERRM);
End KiBEng;
/

No comments: