Boolean Data Type in Oracle Database 23ai/26ai
Oracle database 23ai/26ai introduced the boolean data type in SQL.
oracle 23configurationintermediate
by OracleDba
55 views
Oracle database 23ai/26ai introduced the boolean data type in SQL.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
drop table if exists boolean_test purge;
create table boolean_test (
id number generated always as identity,
active boolean,
archived bool
);
insert into boolean_test (active, archived) values
(true, false),
(TRUE, FALSE),
('true', 'false'),
('TRUE', 'FALSE'),
('yes', 'no'),
('YES', 'NO'),
('on', 'off'),
('ON', 'OFF'),
(1, 0),
('1', '0'),
('t', 'f'),
('T', 'F'),
('y', 'n'),
('Y', 'N');
commit;
select * from boolean_test;
ID ACTIVE ARCHIVED
---------- ----------- -----------
1 TRUE FALSE
2 TRUE FALSE
3 TRUE FALSE
4 TRUE FALSE
5 TRUE FALSE
6 TRUE FALSE
7 TRUE FALSE
8 TRUE FALSE
9 TRUE FALSE
10 TRUE FALSE
11 TRUE FALSE
12 TRUE FALSE
13 TRUE FALSE
14 TRUE FALSE
14 rows selected.
SQL>
select * from boolean_test;
ID ACTIVE ARCHIVED
---------- ---------- ----------
1 1 0
2 1 0
3 1 0
4 1 0
5 1 0
6 1 0
7 1 0
8 1 0
9 1 0
10 1 0
11 1 0
ID ACTIVE ARCHIVED
---------- ---------- ----------
12 1 0
13 1 0
14 1 0
14 rows selected.
SQL>
insert into boolean_test (active, archived) values (null, null);
commit;
select *
from boolean_test
where active is null;
ID ACTIVE ARCHIVED
---------- ----------- -----------
15
SQL>
delete from boolean_test
where active is null;
commit;123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
select count(*)
from boolean_test
where active;
COUNT(*)
----------
14
SQL>
select count(*)
from boolean_test
where active is true;
COUNT(*)
----------
14
SQL>
select count(*)
from boolean_test
where active and not archived;
COUNT(*)
----------
14
SQL>
select count(*)
from boolean_test
where active or archived;
COUNT(*)
----------
14
SQL>
select count(*)
from boolean_test
where active is null;
COUNT(*)
----------
0
SQL>
select count(*)
from boolean_test
where active > archived;
COUNT(*)
----------
14
SQL>
select to_boolean('true'),
to_boolean('false'),
to_number(active),
to_number(archived),
to_char(active),
to_char(archived),
to_nchar(active),
to_nchar(archived)
from boolean_test
where id = 1;
TO_BOOLEAN( TO_BOOLEAN( TO_NUMBER(ACTIVE) TO_NUMBER(ARCHIVED) TO_CH TO_CH TO_NC TO_NC
----------- ----------- ----------------- ------------------- ----- ----- ----- -----
TRUE FALSE 1 0 TRUE FALSE TRUE FALSE
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
declare
l_active boolean := true;
l_archived boolean := false;
begin
insert into boolean_test (active, archived)
values (l_active, l_archived);
commit;
end;
/
PL/SQL procedure successfully completed.
SQL>
declare
l_true boolean;
l_false boolean;
begin
-- Work
l_true := to_boolean('true');
l_false := to_boolean('false');
end;
/
-- Oracle 23.2 only
alter session set plsql_implicit_conversion_bool=true;
declare
l_true boolean := true;
l_false boolean := false;
l_number number;
begin
l_number := to_number(l_true);
l_number := to_number(l_false);
end;
/
declare
l_true boolean;
l_false boolean;
l_string varchar2(5);
begin
l_string := to_char(l_true);
l_string := to_char(l_false);
end;
/
declare
l_true boolean;
l_false boolean;
l_string nvarchar2(5);
begin
l_string := to_nchar(l_true);
l_string := to_nchar(l_false);
end;
/Please to add comments
No comments yet. Be the first to comment!