]> begriffs open source - ai-pg/blob - full-docs/txt/datatype-boolean.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / datatype-boolean.txt
1
2 8.6. Boolean Type #
3
4    PostgreSQL provides the standard SQL type boolean; see Table 8.19. The
5    boolean type can have several states: “true”, “false”, and a third
6    state, “unknown”, which is represented by the SQL null value.
7
8    Table 8.19. Boolean Data Type
9     Name   Storage Size      Description
10    boolean 1 byte       state of true or false
11
12    Boolean constants can be represented in SQL queries by the SQL key
13    words TRUE, FALSE, and NULL.
14
15    The datatype input function for type boolean accepts these string
16    representations for the “true” state:
17    true
18    yes
19    on
20    1
21
22    and these representations for the “false” state:
23    false
24    no
25    off
26    0
27
28    Unique prefixes of these strings are also accepted, for example t or n.
29    Leading or trailing whitespace is ignored, and case does not matter.
30
31    The datatype output function for type boolean always emits either t or
32    f, as shown in Example 8.2.
33
34    Example 8.2. Using the boolean Type
35 CREATE TABLE test1 (a boolean, b text);
36 INSERT INTO test1 VALUES (TRUE, 'sic est');
37 INSERT INTO test1 VALUES (FALSE, 'non est');
38 SELECT * FROM test1;
39  a |    b
40 ---+---------
41  t | sic est
42  f | non est
43
44 SELECT * FROM test1 WHERE a;
45  a |    b
46 ---+---------
47  t | sic est
48
49    The key words TRUE and FALSE are the preferred (SQL-compliant) method
50    for writing Boolean constants in SQL queries. But you can also use the
51    string representations by following the generic string-literal constant
52    syntax described in Section 4.1.2.7, for example 'yes'::boolean.
53
54    Note that the parser automatically understands that TRUE and FALSE are
55    of type boolean, but this is not so for NULL because that can have any
56    type. So in some contexts you might have to cast NULL to boolean
57    explicitly, for example NULL::boolean. Conversely, the cast can be
58    omitted from a string-literal Boolean value in contexts where the
59    parser can deduce that the literal must be of type boolean.