Categories:

Semi-structured and structured data functions (Cast)

AS_DECIMAL , AS_NUMBER¶

Casts a VARIANT value to a fixed-point decimal (does not match floating-point values), with optional precision and scale.

AS_NUMBER is a synonym for AS_DECIMAL.

See also:

AS_<object_type> , AS_DOUBLE , AS_REAL , AS_INTEGER

Syntax¶

AS_DECIMAL( <variant_expr> [ , <precision> [ , <scale> ] ] )

AS_NUMBER( <variant_expr> [ , <precision> [ , <scale> ] ] )
Copy

Arguments¶

variant_expr

An expression that evaluates to a value of type VARIANT.

precision

The number of significant digits of the decimal number to store.

scale

The number of significant digits after the decimal point.

Usage notes¶

  • Default for precision is 38 and scale is 0.

  • When reducing scale, this function rounds, which can cause out-of-range errors.

Examples¶

This shows how to use the function:

Create a table and data:

CREATE TABLE multiple_types (
    binary1 VARIANT,
    date1 VARIANT,
    decimal1 VARIANT,
    time1 VARIANT,
    timestamp1 VARIANT
    );
INSERT INTO multiple_types 
     (binary1, date1, decimal1, time1, timestamp1)
   SELECT 
     TO_VARIANT(TO_BINARY('F0A5')),
     TO_VARIANT(TO_DATE('2018-10-10')), 
     TO_VARIANT(TO_DECIMAL(1.23, 6, 3)),
     TO_VARIANT(TO_TIME('12:34:56')),
     TO_VARIANT(TO_TIMESTAMP_NTZ('2018-10-10 12:34:56'))
     ;
Copy

Now run the query:

SELECT AS_DECIMAL(decimal1, 6, 3) AS "Decimal" FROM multiple_types;
Copy

Output:

+---------+
| Decimal |
|---------|
|   1.230 |
+---------+
Copy