CAST function - Amazon Quantum Ledger Database (Amazon QLDB)

CAST function

Evaluates a given expression to a value and converts the value to a specified target data type. If the conversion can't be made, the function returns an error.

Syntax

CAST ( expression AS type )

Arguments

expression

The field name or expression that evaluates to a value that the function converts. Converting null values returns nulls. This parameter can be any of the supported Data types.

type

The name of the target data type for conversion. This parameter can be one of the supported Data types.

Return type

The data type that is specified by the type argument.

Examples

The following examples show the propagation of unknown types (NULL or MISSING).

CAST(null AS null) -- null CAST(missing AS null) -- null CAST(missing AS missing) -- missing CAST(null AS missing) -- missing CAST(null AS boolean) -- null (null AS any data type name results in null) CAST(missing AS boolean) -- missing (missing AS any data type name results in missing)

Any value that is not an unknown type can't be cast to NULL or MISSING.

CAST(true AS null) -- error CAST(true AS missing) -- error CAST(1 AS null) -- error CAST(1 AS missing) -- error

The following examples show casting AS boolean.

CAST(true AS boolean) -- true no-op CAST(0 AS boolean) -- false CAST(1 AS boolean) -- true CAST(`1e0` AS boolean) -- true (float) CAST(`1d0` AS boolean) -- true (decimal) CAST('a' AS boolean) -- false CAST('true' AS boolean) -- true (SqlName string 'true') CAST(`'true'` AS boolean) -- true (Ion symbol `'true'`) CAST(`'false'` AS boolean) -- false (Ion symbol `'false'`)

The following examples show casting AS integer.

CAST(true AS integer) -- 1 CAST(false AS integer) -- 0 CAST(1 AS integer) -- 1 CAST(`1d0` AS integer) -- 1 CAST(`1d3` AS integer) -- 1000 CAST(1.00 AS integer) -- 1 CAST(1.45 AS integer) -- 1 CAST(1.75 AS integer) -- 1 CAST('12' AS integer) -- 12 CAST('aa' AS integer) -- error CAST(`'22'` AS integer) -- 22 CAST(`'x'` AS integer) -- error

The following examples show casting AS float.

CAST(true AS float) -- 1e0 CAST(false AS float) -- 0e0 CAST(1 AS float) -- 1e0 CAST(`1d0` AS float) -- 1e0 CAST(`1d3` AS float) -- 1000e0 CAST(1.00 AS float) -- 1e0 CAST('12' AS float) -- 12e0 CAST('aa' AS float) -- error CAST(`'22'` AS float) -- 22e0 CAST(`'x'` AS float) -- error

The following examples show casting AS decimal.

CAST(true AS decimal) -- 1. CAST(false AS decimal) -- 0. CAST(1 AS decimal) -- 1. CAST(`1d0` AS decimal) -- 1. (REPL printer serialized to 1.) CAST(`1d3` AS decimal) -- 1d3 CAST(1.00 AS decimal) -- 1.00 CAST('12' AS decimal) -- 12. CAST('aa' AS decimal) -- error CAST(`'22'` AS decimal) -- 22. CAST(`'x'` AS decimal) -- error

The following examples show casting AS timestamp.

CAST(`2001T` AS timestamp) -- 2001T CAST('2001-01-01T' AS timestamp) -- 2001-01-01T CAST(`'2010-01-01T00:00:00.000Z'` AS timestamp) -- 2010-01-01T00:00:00.000Z CAST(true AS timestamp) -- error CAST(2001 AS timestamp) -- error

The following examples show casting AS symbol.

CAST(`'xx'` AS symbol) -- xx (`'xx'` is an Ion symbol) CAST('xx' AS symbol) -- xx ('xx' is a string) CAST(42 AS symbol) -- '42' CAST(`1e0` AS symbol) -- '1.0' CAST(`1d0` AS symbol) -- '1' CAST(true AS symbol) -- 'true' CAST(false AS symbol) -- 'false' CAST(`2001T` AS symbol) -- '2001T' CAST(`2001-01-01T00:00:00.000Z` AS symbol) -- '2001-01-01T00:00:00.000Z'

The following examples show casting AS string.

CAST(`'xx'` AS string) -- "xx" (`'xx'` is an Ion symbol) CAST('xx' AS string) -- "xx" ('xx' is a string) CAST(42 AS string) -- "42" CAST(`1e0` AS string) -- "1.0" CAST(`1d0` AS string) -- "1" CAST(true AS string) -- "true" CAST(false AS string) -- "false" CAST(`2001T` AS string) -- "2001T" CAST(`2001-01-01T00:00:00.000Z` AS string) -- "2001-01-01T00:00:00.000Z"

The following examples show casting AS struct.

CAST(`{ a: 1 }` AS struct) -- {a:1} CAST(true AS struct) -- err

The following examples show casting AS list.

CAST(`[1, 2, 3]` AS list) -- [1,2,3] CAST(<<'a', { 'b':2 }>> AS list) -- ["a",{'b':2}] CAST({ 'b':2 } AS list) -- error

The following examples are runnable statements that include some of the previous examples.

SELECT CAST(true AS integer) FROM << 0 >> -- 1 SELECT CAST('2001-01-01T' AS timestamp) FROM << 0 >> -- 2001-01-01T SELECT CAST('xx' AS symbol) FROM << 0 >> -- xx SELECT CAST(42 AS string) FROM << 0 >> -- "42"

Related functions