All Products
Search
Document Center

PolarDB:Boolean type

Last Updated:Mar 28, 2024

This topic describes the definition and syntax of the boolean type.

Boolean type

Name

Storage Size

Description

boolean

1 byte

state of true or false

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

The datatype input function for type boolean accepts these string representations for the“true”state:

true

yes

on

1

and these representations for the“false”state:

false

no

off

0

Unique prefixes of these strings are also accepted, for example t or n. Leading or trailing whitespace is ignored, and case does not matter.

The datatype output function for typebooleanalways emits eithertorf.

Using the booleantype

    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

The key words TRUE and FALSE are the preferred (SQL-compliant) method for writing Boolean constants in SQL queries.

Important

Note that the parser automatically understands that TRUE and FALSE are of type boolean, but this is not so for NULL because that can have any type. So in some contexts you might have to cast NULL to boolean explicitly, for example NULL::boolean. Conversely, the cast can be omitted from a string-literal Boolean value in contexts where the parser can deduce that the literal must be of typeboolean.