I'm trying to write a function to receive a number and return its binary format. This is what I've developed so far and it's ok for inputs : 4,8,16 but it does not return correct binary format for other numbers.
I could not find the problem and I was wondering if you could find the problem?
create or replace function Show_Binary(i_Number in Number) Return Varchar2
AS
V_Binary varchar2(50) := '';
i_Number2 Number := i_Number;
Begin
While i_Number2>=2 LOOP
V_Binary := V_Binary || Remainder(i_Number2, 2);
i_Number2 := i_Number2 / 2;
End LOOP;
V_Binary := V_Binary || TO_CHAR(i_Number2);
select reverse (V_Binary) into V_Binary from dual;
return (V_Binary);
End;
Best Answer
This is not an Oracle or PL/SQL issue, but a matter of implementing the proper algorithm.
Here is an example:
https://www.orafaq.com/wiki/Binary
The overhead of LISTAGG + hierarchical query + other SQL functions is bigger than the overhead of a simple PL/SQL function.
And this is without the UDF Pragma. On 12c and above the usage of the function gets slightly faster with the addition of
PRAGMA_UDF
.