Is there any Boolean type in Oracle databases, similar to the BIT datatype in Ms SQL Server?
12 Answers
Before version 23c (2023), not only was the boolean datatype missing in Oracle's SQL (not PL/SQL), but they also had no clear recommendation about what to use instead. See this thread on asktom. From recommending CHAR(1) 'Y'/'N' they switch to NUMBER(1) 0/1 when someone points out that 'Y'/'N' depends on the English language, while e.g. German programmers might use 'J'/'N' instead.
The worst thing is that they defend this stupid decision just like they defend the ''=NULL stupidity.
Oracle 23c finally supports Boolean data types in all contexts, along with several other features it has famously lacked compared to other databases.
SQL> select true; TRUE ----------- TRUE SQL> create table test1(a boolean); Table created. SQL> insert into test1 values (true),(false),(to_boolean(0)),(to_boolean('y')); 4 rows created. But the empty string issue will never change.
21 Comments
N and F being used, because ON and OFF begin with the same letter...Nope.
Can use:
IS_COOL NUMBER(1,0) 1 - true 0 - false --- enjoy Oracle
Or use char Y/N as described here
5 Comments
create table testbool (boolc char(1), booln number(1)); insert into testbool values ('Y', 1 ); select dump(boolc), dump(booln) from testbool; That CHAR is stored: Typ=96 Len=1: 89 and that NUMBER: Typ=2 Len=2: 193,2 At least in 12c, NUMBER(1) can use 2 bytes...ResultSet.getBoolean() says: If the designated column has a datatype of CHAR or VARCHAR and contains a "0" or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a 0, a value of false is returned. If the designated column has a datatype of CHAR or VARCHAR and contains a "1" or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a 1, a value of true is returned. -- Based on this, I would recommend the 0/1 solution over Y/N. Even when using a CHAR column, it's better to use numbers.As per Ammoq and kupa's answers, We use number(1) with default of 0 and don't allow nulls.
here's an add column to demonstrate:
ALTER TABLE YourSchema.YourTable ADD (ColumnName NUMBER(1) DEFAULT 0 NOT NULL); Hope this helps someone.
4 Comments
boolean intToBool(int in) { return (in != 0); }ColumnName = 0 or ColumnName = 1, rather than ColumnName = 0 or ColumnName <> 0. The semantics of last one are not programmer friendly. I would also want to keep it more simple for the query optimiser by having two value.No, there isn't a boolean type in Oracle Database, but you can do this way:
You can put a check constraint on a column.
If your table hasn't a check column, you can add it:
ALTER TABLE table_name ADD column_name_check char(1) DEFAULT '1'; When you add a register, by default this column get 1.
Here you put a check that limit the column value, just only put 1 or 0
ALTER TABLE table_name ADD CONSTRAINT name_constraint column_name_check (ONOFF in ( '1', '0' )); Comments
There is a boolean type for use in pl/sql, but none that can be used as the data type of a column.
Comments
A common space-saving trick is storing boolean values as an Oracle CHAR, rather than NUMBER:
3 Comments
If you are using Java with Hibernate then using NUMBER(1,0) is the best approach. As you can see in here, this value is automatically translated to Boolean by Hibernate.
Comments
Just because nobody mentioned it yet: using RAW(1) also seems common practice.
4 Comments
Short answer: to represent Boolean in Oracle, use data type NUMBER(1,0) where 0 represents False and 1 represents True
Long answer: when there are many possible representations or implementations of something, one should choose the one that is (1) most widely adopted, (2) simple, and (3) clean. This is why NUMBER, in my view, is better even than Boolean, introduced in Oracle 23. I have provided detailed explanation here, because there exists another, duplicate thread on the same topic:
https://stackoverflow.com/a/79555398/17329826
Comments
DECLARE error_flag BOOLEAN := false; BEGIN error_flag := true; --error_flag := 13;--expression is of wrong type IF error_flag THEN UPDATE table_a SET id= 8 WHERE id = 1; END IF; END;
ints instead). We should definitely go back to those in code. Additionally, the argument completely falls apart if the data types between table columns and result columns (from aSELECT) are shared, since it is absolutely appropriate to return a boolean as a computed result sometimes even given the rest of the argument.DATEtype - imagine having to deal with string representations of dates all the time :)