- Categories:
TO_BOOLEAN¶
Converts the input text or numeric expression to a BOOLEAN value.
- See also:
Syntax¶
TO_BOOLEAN( <string_or_numeric_expr> )
Arguments¶
string_or_numeric_exprA string expression or numeric expression that can be evaluated to a BOOLEAN value.
Returns¶
Returns a BOOLEAN value or NULL.
Returns TRUE if
string_or_numeric_exprevaluates to TRUE.Returns FALSE if
string_or_numeric_exprevaluates to FALSE.If the input is NULL, returns NULL without reporting an error.
Usage notes¶
For a string expression:
'true','t','yes','y','on','1'return TRUE.'false','f','no','n','off','0'return FALSE.All other strings return an error.
The evaluations of the strings are case-insensitive.
For a numeric expression:
0returns FALSE.All non-zero numeric values return TRUE.
When converting from the FLOAT data type, non-numeric values, such as
NaN(not a number) andINF(infinity), return an error.
Examples¶
The following examples use the TO_BOOLEAN function.
Create a table and insert data:
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 |
+-------+------+------+
Convert a text string to a BOOLEAN value:
SELECT s, TO_BOOLEAN(s) FROM test_boolean;
+------+---------------+
| S | TO_BOOLEAN(S) |
|------+---------------|
| yes | True |
| no | False |
| NULL | NULL |
+------+---------------+
Convert a number to a BOOLEAN value:
SELECT n, TO_BOOLEAN(n) FROM test_boolean;
+------+---------------+
| N | TO_BOOLEAN(N) |
|------+---------------|
| 1 | True |
| 0 | False |
| NULL | NULL |
+------+---------------+