Skip to main content

Cast to BOOLEAN

The BOOLEAN type represents true or false values, with only two possible states: true value and false value.

FROM String​

Behavior Change

Previously, strings like '1.11' could be cast to boolean type 'true', starting from 4.0, they will be converted to null (in non-strict mode) or report an error (in strict mode). Previously, values like 'on', 'off', 'yes', 'no' would be converted to null, starting from 4.0, they can be converted to their corresponding boolean values.

Strict Mode​

BNF Definition​

<boolean> ::= <whitespace>* <bool_like> <whitespace>*

<bool_like> ::= "0" | "1" | "t" | "T" | "f" | "F" | <yes> | <no> | <on> | <off> | <true> | <false>

<yes> ::= ("y" | "Y") ("e" | "E") ("s" | "S")

<no> ::= ("n" | "N") ("o" | "O")

<on> ::= ("o" | "O") ("n" | "N")

<off> ::= ("o" | "O") ("f" | "F") ("f" | "F")

<true> ::= ("t" | "T") ("r" | "R") ("u" | "U") ("e" | "E")

<false> ::= ("f" | "F") ("a" | "A") ("l" | "L") ("s" | "S") ("e" | "E")

<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"

Rule Description​

Boolean values can be in the following forms: 0, 1, yes, no, on, off, true, false, and are case insensitive. Additionally, boolean values can contain any number of whitespace characters before and after (including spaces, tabs, newlines, etc.).

For formats that do not conform, an error is reported.

Examples​

StringCast as bool ResultComment
"true"true
"false"false
" \t\r\n\f\v true \t\r\n\f\v"trueWith leading and trailing whitespace
"1.1"ErrorInvalid format
"YeS"trueCase insensitive
'+0'ErrorInvalid format

Non-Strict Mode​

BNF Definition​

<boolean> ::= <whitespace>* <bool_like> <whitespace>*

<bool_like> ::= "0" | "1" | "t" | "T" | "f" | "F" | <yes> | <no> | <on> | <off> | <true> | <false>

<yes> ::= ("y" | "Y") ("e" | "E") ("s" | "S")

<no> ::= ("n" | "N") ("o" | "O")

<on> ::= ("o" | "O") ("n" | "N")

<off> ::= ("o" | "O") ("f" | "F") ("f" | "F")

<true> ::= ("t" | "T") ("r" | "R") ("u" | "U") ("e" | "E")

<false> ::= ("f" | "F") ("a" | "A") ("l" | "L") ("s" | "S") ("e" | "E")

<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"

Rule Description​

Boolean values can be in the following forms: 0, 1, yes, no, on, off, true, false, and are case insensitive. Additionally, boolean values can contain any number of whitespace characters before and after (including spaces, tabs, newlines, etc.).

For formats that do not conform, null is returned.

Examples​

StringCast as bool ResultComment
"true"true
"false"false
" \t\r\n\f\v true \t\r\n\f\v"trueWith leading and trailing whitespace
"1.1"nullInvalid format
"YeS"trueCase insensitive
'+0'nullInvalid format

FROM Numeric​

Behavior Change

Previously, non-numeric types like date/datetime were allowed to be converted to boolean type, starting from 4.0, this is not supported.

Strict Mode​

Rule Description​

For numeric types (int/double/decimal), non-zero values are considered true.

Positive and negative zeros in floating point numbers are converted to false.

Examples​

Numeric TypeCast as bool ResultComment
121231true
0false
+0.0falsePositive zero in floating point
-0.0falseNegative zero in floating point
-1true
1true

Non-Strict Mode​

Rule Description​

For numeric types (int/double/decimal), non-zero values are considered true.

Positive and negative zeros in floating point numbers are converted to false.

Examples​

Numeric TypeCast as bool ResultComment
121231true
0false
+0.0falsePositive zero in floating point
-0.0falseNegative zero in floating point
-1true
1true