This page in other versions: 8.4 / 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3

3.6. Boolean Type

PostgreSQL provides the SQL99 type boolean. boolean can have one of only two states: "true" or "false". A third state, "unknown", is represented by the SQL NULL state.

Valid literal values for the "true" state are:

TRUE
't'
'true'
'y'
'yes'
'1'
For the "false" state, the following values can be used:
FALSE
'f'
'false'
'n'
'no'
'0'
Using the key words TRUE and FALSE is preferred (and SQL-compliant).

Example 3-2. Using the boolean type

CREATE TABLE test1 (a boolean, b text);
INSERT INTO test1 VALUES (TRUE, 'sic est');
INSERT INTO test1 VALUES (FALSE, 'non est');
SELECT * FROM test1;
 a |    b
---+---------
 t | sic est
 f | non est

SELECT * FROM test1 WHERE a;
 a |    b
---+---------
 t | sic est

Example 3-2 shows that boolean values are output using the letters t and f.

Tip: Values of the boolean type cannot be cast directly to other types (e.g., CAST (boolval AS integer) does not work). This can be accomplished using the CASE expression: CASE WHEN boolval THEN 'value if true' ELSE 'value if false' END. See also Section 4.12.

boolean uses 1 byte of storage.

Comments


July 18, 2002, 1:22 p.m.

Notes for those porting to or from MS SQL . . .
MS SQL doesn't have a 'boolean' data type but has a 'bit' data type that is equivalent to the Postgres 'boolean' data type. The Postgres 'bit' data type is equivalent to the MS SQL 'binary' type.

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group