Skip to main content

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​

StringDecimal(18, 6)Comment
"123.1234567"123.123457Rounding
"12345."12345.000000
"12345"12345.000000
".123456"0.123456
" \t\r\n\f\v123.456 \t\r\n\f\v"123.456000With prefix and suffix whitespace
" \t\r\n\f\v+123.456 \t\r\n\f\v"123.456000With prefix and suffix whitespace, positive sign
" \t\r\n\f\v-123.456 \t\r\n\f\v"-123.456000With prefix and suffix whitespace, negative sign
" \t\r\n\f\v+1.234e5 \t\r\n\f\v"123400.000000Scientific notation
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v"123400.000000Scientific notation with positive exponent
" \t\r\n\f\v+1.234e-1 \t\r\n\f\v"0.123400Scientific notation with negative exponent
"123.456a"ErrorInvalid format
"1234567890123.123456"ErrorOverflow

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​

StringDecimal(18, 6)Comment
"123.1234567"123.123457Rounding
"12345."12345.000000
"12345"12345.000000
".123456"0.123456
" \t\r\n\f\v123.456 \t\r\n\f\v"123.456000With prefix and suffix whitespace
" \t\r\n\f\v+123.456 \t\r\n\f\v"123.456000With prefix and suffix whitespace, positive sign
" \t\r\n\f\v-123.456 \t\r\n\f\v"-123.456000With prefix and suffix whitespace, negative sign
" \t\r\n\f\v+1.234e5 \t\r\n\f\v"123400.000000Scientific notation
" \t\r\n\f\v+1.234e+5 \t\r\n\f\v"123400.000000Scientific notation with positive exponent
" \t\r\n\f\v+1.234e-1 \t\r\n\f\v"0.123400Scientific notation with negative exponent
"123.456a"NULLInvalid format
"1234567890123.123456"NULLOverflow

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​

intDecimal(18, 9)Comment
123123.00000000
2147483647ErrorOverflow

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​

intDecimal(18, 9)Comment
123123.00000000
2147483647NULLOverflow

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/doubleDecimal(18, 3)Comment
1.12391.124Rounding
3.40282e+38ErrorOverflow
InfinityError
NaNError

Non-strict mode​

Always returns nullable type.

  • +/-Inf converts to NULL;

  • NaN converts to NULL;

  • Converts to NULL when overflow occurs.

Examples​

float/doubleDecimal(18, 6)Comment
1.1234561.123456
3.40282e+38NULLOverflow
InfinityNULL
NaNNULL

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.123456781234.123457Rounding
12345.12345678ErrorInteger 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.123456781234.123457Rounding
12345.12345678NULLInteger part overflow

From date​

Not supported.

From datetime​

Not supported.

From time​

Not supported.

From other types​

Not supported