Boolean type

更新时间:
复制 MD 格式

The boolean data type represents three states: TRUE, FALSE, and an unknown state represented by NULL.

Data type overview

NameStorage sizeDescription
boolean1 byteStores true, false, or unknown (NULL)

Valid literals

Boolean constants in SQL queries can be represented by the SQL keywords TRUE, FALSE, and NULL.

Use the TRUE and FALSE SQL keywords as the preferred (SQL-compliant) way to write Boolean constants in SQL queries.

The input function also accepts these string representations:

StateAccepted strings
Truetrue, yes, on, 1
Falsefalse, no, off, 0

Unique prefixes of these strings are also accepted — for example, t or n. Matching is case-insensitive, and leading or trailing whitespace is ignored.

The output function always emits t or f, regardless of which input form was used.

NULL and type casting

Important

The parser recognizes TRUE and FALSE as Boolean values automatically. NULL has no implicit type — cast it explicitly when the context requires a Boolean: NULL::boolean. Conversely, you can omit the cast from a string literal when the parser can infer the type from context.

Example

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