# #100CodeExamples - BOOLEAN to INT

• Script Name #100CodeExamples - BOOLEAN to INT
• Visibility Unlisted - anyone with the share link can access
• Description Boolean to Int conversion in PL/SQL - different approaches.
• Area PL/SQL General
• Contributor Pesse
• Created Friday January 11, 2019
• Statement 1
``````declare
-- Use a straight if-else approach
function bool_int_if ( i_bool boolean ) return int
as
begin
if ( i_bool ) then
return 1;
else
return 0;
end if;
end;

-- Use case-when-else
function bool_int_case( i_bool boolean ) return int
as
begin
return case i_bool when true then 1 else 0 end;
end;

-- Use the bool_to_int-function of sys.diutil-package
function bool_int_diutil( i_bool boolean ) return int
as
begin
return sys.diutil.bool_to_int(i_bool);
end;

-- Use sys.diutil and nvl to deal with NULL-values
function bool_int_diutil_nvl( i_bool boolean ) return int
as
begin
return nvl(sys.diutil.bool_to_int(i_bool),0);
end;
begin

dbms_output.put_line(
'IF-approach: ' ||
bool_int_if(true) || ', ' ||
bool_int_if(false) || ', ' ||
bool_int_if(null)
);

dbms_output.put_line(
'CASE-approach: ' ||
bool_int_case(true) || ', ' ||
bool_int_case(false) || ', ' ||
bool_int_case(null)
);

dbms_output.put_line(
'DIUTIL-approach: ' ||
bool_int_diutil(true) || ', ' ||
bool_int_diutil(false) || ', ' ||
bool_int_diutil(null)
);

dbms_output.put_line(
'DIUTIL with NVL-approach: ' ||
bool_int_diutil_nvl(true) || ', ' ||
bool_int_diutil_nvl(false) || ', ' ||
bool_int_diutil_nvl(null)
);

end;``````
ORA-06550: line 24, column 14: PLS-00201: identifier 'SYS.DIUTIL' must be declared
• Statement 2
``````declare
-- Use a straight if-else approach
function bool_int_if ( i_bool boolean ) return int
as
begin
if ( i_bool ) then
return 1;
else
return 0;
end if;
end;

-- Use case-when-else
function bool_int_case( i_bool boolean ) return int
as
begin
return case i_bool when true then 1 else 0 end;
end;

begin

dbms_output.put_line(
'IF-approach: ' ||
bool_int_if(true) || ', ' ||
bool_int_if(false) || ', ' ||
bool_int_if(null)
);

dbms_output.put_line(
'CASE-approach: ' ||
bool_int_case(true) || ', ' ||
bool_int_case(false) || ', ' ||
bool_int_case(null)
);

end;``````
Statement processed.
IF-approach: 1, 0, 0
CASE-approach: 1, 0, 0