A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/data-type-conversion below:

Website Navigation


Data type conversion | Snowflake Documentation

Data type conversion

In many cases, a value of one data type can be converted to another data type. For example, an INTEGER value can be converted to a floating-point data type value. Converting a data type is called casting.

Explicit casting vs implicit casting

Users can explicitly convert a value from one data type to another. This is called explicit casting.

In some situations, Snowflake converts a value to another data type automatically. This is called implicit casting or coercion.

Explicit casting

Users can explicitly cast a value by using any of the following options:

For example, each query casts a string value to a DATE value:

SELECT CAST('2022-04-01' AS DATE);

SELECT '2022-04-01'::DATE;

SELECT TO_DATE('2022-04-01');

Copy

Casting is allowed in most contexts in which a general expression is allowed, including the WHERE clause. For example:

SELECT date_column
  FROM log_table
  WHERE date_column >= '2022-04-01'::DATE;

Copy

Implicit casting (coercion)

Coercion occurs when a function (or operator) requires a data type that is different from, but compatible with, the arguments (or operands).

Not all contexts (for example, not all operators) support coercion.

Casting and precedence

When casting inside an expression, the code must take into account the precedence of the cast operator relative to other operators in the expression.

Consider the following example:

SELECT height * width::VARCHAR || ' square meters'
  FROM dimensions;

Copy

The cast operator has higher precedence than the arithmetic operator * (multiply), so the statement is interpreted as:

... height * (width::VARCHAR) ...

Copy

To cast the result of the expression height * width, use parentheses, as shown below:

SELECT (height * width)::VARCHAR || ' square meters'
  FROM dimensions;

Copy

As another example, consider the following statement:

SELECT -0.0::FLOAT::BOOLEAN;

Copy

You might expect this to be interpreted as:

SELECT (-0.0::FLOAT)::BOOLEAN;

Copy

Therefore, it would be expected to return FALSE (0 = FALSE, 1 = TRUE).

However, the cast operator has higher precedence than the unary minus (negation) operator, so the statement is interpreted as:

SELECT -(0.0::FLOAT::BOOLEAN);

Copy

Therefore, the query results in an error message because the unary minus can’t be applied to a BOOLEAN.

Data types that can be cast

The following table shows the valid data type conversions in Snowflake. The table also shows which coercions Snowflake can perform automatically.

Note

Internally, the CAST function and the :: operator call the appropriate conversion function. For example, if you cast a NUMBER to a BOOLEAN, then Snowflake calls the TO_BOOLEAN function. The usage notes for each conversion function apply when the function is called indirectly using a cast, as well as when the function is called directly. For example, if you execute CAST(my_decimal_column AS BOOLEAN), the rules for calling TO_BOOLEAN with a DECIMAL value apply. For convenience, the table includes links to the relevant conversion functions.

For details on conversions between semi-structured types and structured types, see Converting structured and semi-structured types.

Source data type

Target data type

Castable

Coercible

Conversion function

Notes

ARRAY

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

VECTOR

Use explicit casting for conversion. For more information, see Vector conversion.

BINARY

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

BOOLEAN

NUMBER

TO_NUMBER

VARCHAR

TO_VARCHAR

For example, from TRUE to 'true'.

VARIANT

TO_VARIANT

DATE

TIMESTAMP

TO_TIMESTAMP

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

FLOAT . (floating-point numbers)

BOOLEAN

TO_BOOLEAN

For example, from 0.0 to FALSE.

NUMBER[(p,s)]

TO_NUMBER

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

GEOGRAPHY

VARIANT

TO_VARIANT

GEOMETRY

VARIANT

TO_VARIANT

NUMBER[(p,s)] . (Fixed-point numbers, including INTEGER)

BOOLEAN

TO_BOOLEAN

For example, from 0 to FALSE.

FLOAT

TO_DOUBLE

TIMESTAMP

TO_TIMESTAMP

[1]

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

OBJECT

ARRAY

TO_ARRAY

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

TIME

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

TIMESTAMP

DATE

TO_DATE , DATE

TIME

TO_TIME , TIME

VARCHAR

TO_VARCHAR

VARIANT

TO_VARIANT

VARCHAR

BOOLEAN

TO_BOOLEAN

For example, from 'false' to FALSE.

DATE

TO_DATE , DATE

FLOAT

TO_DOUBLE

For example, from '12.34' to 12.34.

NUMBER[(p,s)]

TO_NUMBER

For example, from '12.34' to 12.34.

TIME

TO_TIME , TIME

TIMESTAMP

TO_TIMESTAMP

VARIANT

TO_VARIANT

VARIANT

ARRAY

TO_ARRAY

BOOLEAN

TO_BOOLEAN

For example, from a VARIANT containing 'false' to FALSE.

DATE

TO_DATE , DATE

FLOAT

TO_DOUBLE

GEOGRAPHY

TO_GEOGRAPHY

NUMBER[(p,s)]

TO_NUMBER

OBJECT

TO_OBJECT

TIME

TO_TIME , TIME

TIMESTAMP

TO_TIMESTAMP

VARCHAR

TO_VARCHAR

VECTOR

The VARIANT must contain an ARRAY of type FLOAT or INT.

VECTOR

ARRAY

TO_ARRAY

Note

For each listed data type (for example, FLOAT), the rules apply to all aliases for that data type (for example, the rules for FLOAT apply to DOUBLE, which is an alias for FLOAT).

Usage notes

Except where stated otherwise, the following rules apply to both explicit casting and implicit casting:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4