TO_DECIMAL
functionsupportedCore SQL functionsConverts an input expression to a fixed-point number. These functions are synonymous.
Syntax
supportedpartialnot supportednot scored
TO_DECIMAL( <expr> [, '<format>' ] [, <precision> [, <scale> ] ] )
TO_NUMBER( <expr> [, '<format>' ] [, <precision> [, <scale> ] ] )
TO_NUMERIC( <expr> [, '<format>' ] [, <precision> [, <scale> ] ] )
Parameters
| Name | Required | Description |
|---|---|---|
expr | required | An expression of a numeric, character, or variant type. |
format | optional | The SQL format model used to parse the input expr and return. For more information, see SQL format models. |
precision | optional | The maximum number of decimal digits in the resulting number, from 1 to 38. In Snowflake, precision isn’t used to determine the number of bytes that are needed to store the number and doesn’t have any effect on efficiency, so the default is the maximum (38). |
scale | optional | The number of fractional decimal digits (from 0 to precision - 1). 0 indicates no fractional digits; that is, an integer number. The default scale is 0. |
Returns
The function returns a value of type NUMBER with the following defaults: - If the precision isn’t specified, then it defaults to 38. - If the scale isn’t specified, then it defaults to 0. For NULL input, returns NULL.
Usage notes
- For fixed-point numbers: Numbers with different scales are converted by either adding zeros to the right (if the scale needs to be increased) or by reducing the number of fractional digits by rounding (if the scale needs to be decreased).
- Note that casts of fixed-point numbers to fixed-point numbers that increase scale might fail.
- For floating-point numbers: Numbers are converted if they are within the representable range, given the scale.
- The conversion between binary and decimal fractional numbers is not precise. This might result in loss of precision or out-of-range errors.
- Values of infinity and NaN (not-a-number) result in conversion errors.
- Strings are converted as decimal, integer, fractional, or floating-point numbers. For fractional input, the precision is deduced as the number of digits after the point.
- For floating-point input, omitting the mantissa or exponent is allowed and is interpreted as 0. Thus, E is parsed as 0.
- For VARIANT input: If the variant contains a fixed-point or a floating-point numeric value, an appropriate numeric conversion is performed.
- If the variant contains a string, a string conversion is performed.
- If the variant contains a Boolean value, the result is 0 or 1 (for false and true, correspondingly).
- If the variant contains JSON null value, the output is NULL.