Casts
Type conversions — the scalar type names are callable as conversion functions: int(x), float(x), string(x), bool(x), number(x). They desugar to per-type builtins, so null propagates (a null cast is null), bad conversions are runtime errors, and a non-scalar target name (object, array, any) is a syntax error. Float→int truncates toward zero; string→number parsing is lenient (trims whitespace, accepts float syntax for int targets). Note: MonaDB has no unary minus, so negatives are written as literals.
Int(x) Over
Int(x) over int, float, string, and bool sources.
SQL
select int(7);
Result
[
7
]
SQL
select int(7);
select int(2.7);
Result
[
2
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
Result
[
-2
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
Result
[
2
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
Result
[
5
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
select int(' 5 ');
Result
[
5
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
select int(' 5 ');
select int('2.7');
Result
[
2
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
select int(' 5 ');
select int('2.7');
select int(true);
Result
[
1
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
select int(' 5 ');
select int('2.7');
select int(true);
select int(false);
Result
[
0
]
SQL
select int(7);
select int(2.7);
select int(-2.7);
select int(2.0);
select int('5');
select int(' 5 ');
select int('2.7');
select int(true);
select int(false);
select int('9007199254740993.0') = 9007199254740993;
Result
[
true
]
Float(x) Over
Float(x) over int, float, string, and bool sources.
SQL
select float(3);
Result
[
3
]
SQL
select float(3);
select float(2.5);
Result
[
2.5
]
SQL
select float(3);
select float(2.5);
select float('1.5');
Result
[
1.5
]
SQL
select float(3);
select float(2.5);
select float('1.5');
select float('4');
Result
[
4
]
SQL
select float(3);
select float(2.5);
select float('1.5');
select float('4');
select float(true);
Result
[
1
]
SQL
select float(3);
select float(2.5);
select float('1.5');
select float('4');
select float(true);
select float(false);
Result
[
0
]
String(x) Renders
String(x) renders a scalar as text.
SQL
select string(42);
Result
[
"42"
]
SQL
select string(42);
select string(1.5);
Result
[
"1.5"
]
SQL
select string(42);
select string(1.5);
select string(true);
Result
[
"true"
]
SQL
select string(42);
select string(1.5);
select string(true);
select string(false);
Result
[
"false"
]
SQL
select string(42);
select string(1.5);
select string(true);
select string(false);
select string('hi');
Result
[
"hi"
]
Bool(x) Over
Bool(x) over numbers and the true/false strings.
SQL
select bool(0);
Result
[
false
]
SQL
select bool(0);
select bool(1);
Result
[
true
]
SQL
select bool(0);
select bool(1);
select bool(5);
Result
[
true
]
SQL
select bool(0);
select bool(1);
select bool(5);
select bool(0.0);
Result
[
false
]
SQL
select bool(0);
select bool(1);
select bool(5);
select bool(0.0);
select bool(2.5);
Result
[
true
]
SQL
select bool(0);
select bool(1);
select bool(5);
select bool(0.0);
select bool(2.5);
select bool('true');
Result
[
true
]
SQL
select bool(0);
select bool(1);
select bool(5);
select bool(0.0);
select bool(2.5);
select bool('true');
select bool('false');
Result
[
false
]
SQL
select bool(0);
select bool(1);
select bool(5);
select bool(0.0);
select bool(2.5);
select bool('true');
select bool('false');
select bool('TRUE');
Result
[
true
]
Number(x) Keeps
Number(x) keeps the int/float-ness of the value.
SQL
select number(7);
Result
[
7
]
SQL
select number(7);
select number(2.5);
Result
[
2.5
]
SQL
select number(7);
select number(2.5);
select number('7');
Result
[
7
]
SQL
select number(7);
select number(2.5);
select number('7');
select number('1.5');
Result
[
1.5
]
SQL
select number(7);
select number(2.5);
select number('7');
select number('1.5');
select number(true);
Result
[
1
]
SQL
select number(7);
select number(2.5);
select number('7');
select number('1.5');
select number(true);
select number('5.0');
Result
[
5
]
Cast Compose
A cast is a normal call — it nests and combines with operators.
SQL
select int('5') + 1;
Result
[
6
]
SQL
select int('5') + 1;
select int(2.9) + 1;
Result
[
3
]
SQL
select int('5') + 1;
select int(2.9) + 1;
select int({a: 2.9}.a);
Result
[
2
]
SQL
select int('5') + 1;
select int(2.9) + 1;
select int({a: 2.9}.a);
select int(float('2.7'));
Result
[
2
]
Cast null
A null argument short-circuits to null.
SQL
select int(null);
Result
[
null
]
SQL
select int(null);
select float(null);
Result
[
null
]
SQL
select int(null);
select float(null);
select string(null);
Result
[
null
]
SQL
select int(null);
select float(null);
select string(null);
select bool(null);
Result
[
null
]
SQL
select int(null);
select float(null);
select string(null);
select bool(null);
select number(null);
Result
[
null
]
Typeof(t(x)) Reports
Typeof(t(x)) reports the target type.
SQL
select typeof(int(2.7));
Result
[
"int"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
Result
[
"float"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
Result
[
"int"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
Result
[
"float"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
Result
[
"string"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
select typeof(bool(1));
Result
[
"bool"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
select typeof(bool(1));
select typeof(float('4'));
Result
[
"float"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
select typeof(bool(1));
select typeof(float('4'));
select typeof(number('7'));
Result
[
"int"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
select typeof(bool(1));
select typeof(float('4'));
select typeof(number('7'));
select typeof(number('5.0'));
Result
[
"float"
]
SQL
select typeof(int(2.7));
select typeof(float(5));
select typeof(number(5));
select typeof(number(2.5));
select typeof(string(1));
select typeof(bool(1));
select typeof(float('4'));
select typeof(number('7'));
select typeof(number('5.0'));
select typeof(number(true));
Result
[
"int"
]
Bad Conversions
Bad conversions fail at runtime.
SQL
select int('abc');
Expected error: runtime
SQL
select int('abc');
select int([1]);
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
select int('inf');
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
select int('inf');
select float('nan');
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
select int('inf');
select float('nan');
select float('inf');
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
select int('inf');
select float('nan');
select float('inf');
select string([1, 2]);
Expected error: runtime
SQL
select int('abc');
select int([1]);
select bool('x');
select int(1e19);
select float([1, 2]);
select int('inf');
select float('nan');
select float('inf');
select string([1, 2]);
select string({a: 1});
Expected error: runtime
Object/array/any Are
Object/array/any are not callable conversion functions.
SQL
select object(1);
Expected error: syntax
SQL
select object(1);
select array(1);
Expected error: syntax
SQL
select object(1);
select array(1);
select any(1);
Expected error: syntax