Categories:

Conversion Functions

TO_BOOLEAN¶

Coverts the input text or numeric expression to a Boolean value. For NULL input, the output is NULL.

See also:

TRY_TO_BOOLEAN

Syntax¶

TO_BOOLEAN( <text_or_numeric_expr> )
Copy

Usage Notes¶

  • For a text expression, the string must be:

    • 'true', 't', 'yes', 'y', 'on', '1' return TRUE.

    • 'false', 'f', 'no', 'n', 'off', '0' return FALSE.

    • All other strings return an error.

    Strings are case-insensitive.

  • For a numeric expression:

    • 0 returns FALSE.

    • All non-zero numeric values return TRUE.

    • When converting from the FLOAT data type, non-numeric values, such as ‘NaN’ (not a number) and ‘INF’ (infinity), cause an error.

Examples¶

CREATE OR REPLACE TABLE test_boolean(
   b BOOLEAN,
   n NUMBER,
   s STRING);

INSERT INTO test_boolean VALUES (true, 1, 'yes'), (false, 0, 'no'), (null, null, null);

SELECT * FROM test_boolean;

+-------+------+------+
| B     |    N | S    |
|-------+------+------|
| True  |    1 | yes  |
| False |    0 | no   |
| NULL  | NULL | NULL |
+-------+------+------+
Copy

Convert a text string to boolean:

SELECT s, TO_BOOLEAN(s) FROM test_boolean;

+------+---------------+
| S    | TO_BOOLEAN(S) |
|------+---------------|
| yes  | True          |
| no   | False         |
| NULL | NULL          |
+------+---------------+
Copy

Convert a number to boolean:

SELECT n, TO_BOOLEAN(n) FROM test_boolean;

+------+---------------+
|    N | TO_BOOLEAN(N) |
|------+---------------|
|    1 | True          |
|    0 | False         |
| NULL | NULL          |
+------+---------------+
Copy