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);
insert into boolean_test (active, archived) values (TRUE, FALSE);
insert into boolean_test (active, archived) values ('true', 'false');
insert into boolean_test (active, archived) values ('TRUE', 'FALSE');
insert into boolean_test (active, archived) values ('yes', 'no');
insert into boolean_test (active, archived) values ('YES', 'NO');
insert into boolean_test (active, archived) values ('on', 'off');
insert into boolean_test (active, archived) values ('ON', 'OFF');
insert into boolean_test (active, archived) values (1, 0);
insert into boolean_test (active, archived) values ('1', '0');
insert into boolean_test (active, archived) values ('t', 'f');
insert into boolean_test (active, archived) values ('T', 'F');
insert into boolean_test (active, archived) values ('y', 'n');
insert into boolean_test (active, archived) values ('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.
insert into boolean_test (active, archived) values (null, null);
commit;
select *
from boolean_test
where active is null;
ID ACTIVE ARCHIVED
---------- ----------- -----------
15
delete from boolean_test
where active is null;
commit;
select count(*)
from boolean_test
where active;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active is true;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active and not archived;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active or archived;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active is null;
COUNT(*)
----------
0
select count(*)
from boolean_test
where active > archived;
COUNT(*)
----------
14
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
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.
declare
l_true boolean;
l_false boolean;
begin
-- Work
l_true := to_boolean('true');
l_false := to_boolean('false');
end;
/
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;
/
原文作者:SQL编程思想;如有侵权,请联系管理员删除。