Cast to DECIMAL
From stringβ
Strict modeβ
If the source type is nullable, returns nullable type;
If the source type is non-nullable, returns non-nullable type;
BNF definitionβ
<decimal> ::= <whitespace>* <value> <whitespace>*
<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
<value> ::= <sign>? <significand> <exponent>?
<sign> ::= "+" | "-"
<significand> ::= <digits> "." <digits> | <digits> | <digits> "." | "." <digits>
<digits> ::= <digit>+
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<exponent> ::= <e_marker> <sign>? <digits>
<e_marker> ::= "e" | "E"
Rule descriptionβ
-
Only supports decimal digits;
-
Supports scientific notation;
-
Supports rounding;
-
Strings allow arbitrary prefix and suffix whitespace characters, including: " ", "\t", "\n", "\r", "\f", "\v".
-
Return error when integer part overflows;
-
Return error for invalid format.
Examplesβ
String | Decimal(18, 6) | Comment |
---|---|---|
"123.1234567" | 123.123457 | Rounding |
"12345." | 12345.000000 | |
"12345" | 12345.000000 | |
".123456" | 0.123456 | |
" \t\r\n\f\v123.456 \t\r\n\f\v" | 123.456000 | With prefix and suffix whitespace |
" \t\r\n\f\v+123.456 \t\r\n\f\v" | 123.456000 | With prefix and suffix whitespace, positive sign |
" \t\r\n\f\v-123.456 \t\r\n\f\v" | -123.456000 | With prefix and suffix whitespace, negative sign |
" \t\r\n\f\v+1.234e5 \t\r\n\f\v" | 123400.000000 | Scientific notation |
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v" | 123400.000000 | Scientific notation with positive exponent |
" \t\r\n\f\v+1.234e-1 \t\r\n\f\v" | 0.123400 | Scientific notation with negative exponent |
"123.456a" | Error | Invalid format |
"1234567890123.123456" | Error | Overflow |
Non-strict modeβ
Always returns nullable type;
BNF definitionβ
<decimal> ::= <whitespace>* <value> <whitespace>*
<whitespace> ::= " " | "\t" | "\n" | "\r" | "\f" | "\v"
<value> ::= <sign>? <significand> <exponent>
<sign> ::= "+" | "-"
<significand> ::= <digits> | <digits> "." <digits> | <digits> "." | "." <digits>
<digits> ::= <digit>+
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<exponent> ::= <e_marker> <sign>? <digits>
<e_marker> ::= "e" | "E"
Rule descriptionβ
-
Supports all valid formats from strict mode;
-
Converts to NULL when overflow occurs;
-
Converts to NULL for invalid format.
Examplesβ
String | Decimal(18, 6) | Comment |
---|---|---|
"123.1234567" | 123.123457 | Rounding |
"12345." | 12345.000000 | |
"12345" | 12345.000000 | |
".123456" | 0.123456 | |
" \t\r\n\f\v123.456 \t\r\n\f\v" | 123.456000 | With prefix and suffix whitespace |
" \t\r\n\f\v+123.456 \t\r\n\f\v" | 123.456000 | With prefix and suffix whitespace, positive sign |
" \t\r\n\f\v-123.456 \t\r\n\f\v" | -123.456000 | With prefix and suffix whitespace, negative sign |
" \t\r\n\f\v+1.234e5 \t\r\n\f\v" | 123400.000000 | Scientific notation |
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v" | 123400.000000 | Scientific notation with positive exponent |
" \t\r\n\f\v+1.234e-1 \t\r\n\f\v" | 0.123400 | Scientific notation with negative exponent |
"123.456a" | NULL | Invalid format |
"1234567890123.123456" | NULL | Overflow |
From boolβ
true converts to 1, false converts to 0.
Strict modeβ
Error when overflow occurs (e.g., cast bool as decimal(1, 1)
).
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Non-strict modeβ
Converts to NULL when overflow occurs.
If the source type is nullable, returns nullable type.
If the source type is non-nullable:
-
If overflow is possible (e.g.,
cast bool as decimal(1, 1)
), returns nullable type; -
Otherwise returns non-nullable type.
From integerβ
Strict modeβ
Error when overflow occurs.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
int | Decimal(18, 9) | Comment |
---|---|---|
123 | 123.00000000 | |
2147483647 | Error | Overflow |
Non-strict modeβ
Converts to NULL when overflow occurs.
If the source type is nullable, returns nullable type.
If the source type is non-nullable:
-
If overflow is possible (e.g.,
cast int as decimal(1, 0)
), returns nullable type; -
Otherwise returns non-nullable type (e.g.,
cast int as decimal(18, 0)
).
Examplesβ
int | Decimal(18, 9) | Comment |
---|---|---|
123 | 123.00000000 | |
2147483647 | NULL | Overflow |
From float/doubleβ
Supports rounding.
Strict modeβ
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
-
Infinity and NaN cause errors.
-
Error when overflow occurs.
Examplesβ
float/double | Decimal(18, 3) | Comment |
---|---|---|
1.1239 | 1.124 | Rounding |
3.40282e+38 | Error | Overflow |
Infinity | Error | |
NaN | Error |
Non-strict modeβ
Always returns nullable type.
-
+/-Inf converts to NULL;
-
NaN converts to NULL;
-
Converts to NULL when overflow occurs.
Examplesβ
float/double | Decimal(18, 6) | Comment |
---|---|---|
1.123456 | 1.123456 | |
3.40282e+38 | NULL | Overflow |
Infinity | NULL | |
NaN | NULL |
Cast between decimalsβ
Supports rounding.
Strict modeβ
Error when overflow occurs.
If the source type is nullable, returns nullable type.
If the source type is non-nullable, returns non-nullable type.
Examplesβ
Decimal(18, 8) | Decimal(10, 6) | Comment |
---|---|---|
1234.12345678 | 1234.123457 | Rounding |
12345.12345678 | Error | Integer part overflow |
Non-strict modeβ
Converts to NULL when overflow occurs.
If the source type is nullable, returns nullable type.
If the source type is non-nullable:
-
If overflow is possible (e.g.,
cast decimal(18, 0) as decimal(9, 0)
), returns nullable type; -
Otherwise returns non-nullable type (e.g.,
cast decimal(9, 0) as decimal(18, 0)
).
Examplesβ
Decimal(18, 8) | Decimal(10, 6) | Comment |
---|---|---|
1234.12345678 | 1234.123457 | Rounding |
12345.12345678 | NULL | Integer part overflow |
From dateβ
Not supported.
From datetimeβ
Not supported.
From timeβ
Not supported.
From other typesβ
Not supported