A RetroSearch Logo

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

Search Query:

Showing content from https://docs.databricks.com/aws/en/sql/language-manual/functions/cast below:

cast function | Databricks Documentation

cast function

Applies to: Databricks SQL Databricks Runtime

Casts the value expr to the target data type type. This operator is a synonym for :: (colon colon sign) operator

Syntax​
cast(sourceExpr AS targetType)
Arguments​ Returns​

The result is type targetType.

The following combinations of data type casting are valid:

Rules and limitations based on targetType​

warning

In Databricks Runtime, if spark.sql.ansi.enabled is false, an overflow will not cause an error but instead will “wrap” the result.

A sourceExpr value with an invalid format or invalid characters for targetType will result in a NULL.

numeric​

If the targetType is a numeric and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS INT);
NULL

> SELECT cast(5.6 AS INT);
5

> SELECT cast(5.6 AS DECIMAL(2, 0));
6

> SELECT cast(-5.6 AS INT);
-5

> SELECT cast(-5.6 AS DECIMAL(2, 0));
-6

> SELECT cast(128 AS TINYINT);
Overflow

> SELECT cast(128 AS DECIMAL(2, 0));
Overflow

> SELECT cast('123' AS INT);
123

> SELECT cast('123.0' AS INT);
Invalid format

> SELECT cast(TIMESTAMP'1970-01-01 00:00:01' AS LONG);
1

> SELECT cast(TIMESTAMP'1970-01-01 00:00:00.000001' AS DOUBLE);
1.0E-6

> SELECT cast(TIMESTAMP'2022-02-01 00:00:00' AS SMALLINT);
error: overflow
> SELECT cast(true AS BOOLEAN);
1

> SELECT cast(INTERVAL '1-2' YEAR TO MONTH AS INTEGER);
14

> SELECT cast(INTERVAL '1:30.5' MINUTE TO SECOND AS DECIMAL(5, 2));
90.50

> SELECT cast(TRUE AS INT);
1

> SELECT cast(FALSE AS INT);
0

> SELECT cast('15'::VARIANT AS INT);
15
STRING​

If the sourceExpr is a STRING the resulting STRING inherits the collation of sourceExpr. In all other cases the collation of the resulting STRING is the default collation.

To change the collation add the collate expression.

If the targetType is a STRING type and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS STRING);
NULL

> SELECT cast(-3Y AS STRING);
-3

> SELECT cast(5::DECIMAL(10, 5) AS STRING);
5.00000

> SELECT cast(12345678e-4 AS STRING);
1234.5678

> SELECT cast(1e7 as string);
1.0E7

> SELECT cast(1e6 as string);
1000000.0

> SELECT cast(1e-4 as string);
1.0E-4

> SELECT cast(1e-3 as string);
0.001

> SELECT cast(12345678e7 AS STRING);
1.2345678E14

> SELECT cast(DATE'1900-12-31' AS STRING);
1900-12-31


> SELECT cast(DATE'-0044-03-15' AS STRING);
-0044-03-15

> SELECT cast(DATE'100000-12-31' AS STRING);
+100000-12-31

> SELECT cast(current_timestamp() AS STRING);
2022-04-02 22:29:09.783

> SELECT cast(TIMESTAMP_NTZ'2023-01-01' AS STRING);
2023-01-01 00:00:00

> SELECT cast(INTERVAL -'13-02' YEAR TO MONTH AS STRING);
INTERVAL '-13-2' YEAR TO MONTH

> SELECT cast(INTERVAL '12:04.9900' MINUTE TO SECOND AS STRING);
INTERVAL '12:04.99' MINUTE TO SECOND

> SELECT cast(true AS STRING);
true

> SELECT cast(false AS STRING);
false


> SELECT cast(x'33800033' AS STRING);
3�3

> SELECT hex(cast(x'33800033' AS STRING));
33800033

> SELECT cast(array('hello', NULL, 'world') AS STRING);
[hello, null, world]

> SELECT cast(array('hello', 'wor, ld') AS STRING);
[hello, wor, ld]

> SELECT cast(array() AS STRING);
[]

> SELECT cast(map('hello', 1, 'world', null) AS STRING);
{hello -> 1, world -> null}

> SELECT cast(map('hello -> 1', DATE'2022-01-01') AS STRING);
{hello -> 1 -> 2022-01-01}

> SELECT cast(map() AS STRING);
{}

> SELECT cast(named_struct('a', 5, 'b', 6, 'c', NULL) AS STRING);
{5, 6, null}

> SELECT cast(named_struct() AS STRING);
{}

> SELECT cast(DATE'2024-01-05'::VARIANT AS STRING);
2024-01-05

> SELECT cast(5 AS STRING) COLLATE UNICODE;
5
DATE​

If the targetType is a DATE type and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS DATE);
NULL

> SELECT cast('1900-10-01' AS DATE);
1900-10-01

> SELECT cast('1900-10-01' AS DATE);
1900-10-01


> SELECT cast('1900-02-30' AS DATE);
Error

> SELECT cast(TIMESTAMP'1900-10-01 12:13:14' AS DATE);
1900-10-01

> SELECT cast(TIMESTAMP_NTZ'1900-10-01 12:13:14' AS DATE);
1900-10-01

> SELECT cast(TIMESTAMP_NTZ'1900-10-01 12:13:14'::VARIANT AS DATE);
1900-10-01
TIMESTAMP​

If the targetType is a TIMESTAMP type and sourceExpr is of type:

The result is a timestamp value with the same year/month/day/hour/minute/second fields of timestamp_ntz sourceExpr.

Examples​

SQL

> SELECT cast(NULL AS TIMESTAMP);
NULL

> SET TIME ZONE '+00:00';
> SELECT cast(0.0 AS TIMESTAMP);
1970-01-01 00:00:00

> SELECT cast(0.0000009 AS TIMESTAMP);
1970-01-01 00:00:00

> SELECT cast(1e20 AS TIMESTAMP);
Error: overflow

> SELECT cast('1900' AS TIMESTAMP);
1900-01-01 00:00:00

> SELECT cast('1900-10-01 12:13:14' AS TIMESTAMP);
1900-10-01 12:13:14

> SELECT cast('1900-02-30 12:13:14' AS TIMESTAMP);
Error

> SELECT cast(DATE'1900-10-01' AS TIMESTAMP);
1900-10-01 00:00:00

> SELECT cast(TIMESTAMP_NTZ'2023-01-01 02:03:04.567' as TIMESTAMP)
2023-01-01 02:03:04.567

> SELECT cast(DATE'1900-10-01'::VARIANT AS TIMESTAMP);
1900-10-01 00:00:00
TIMESTAMP_NTZ​

If the targetType is a TIMESTAMP_NTZ type and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS TIMESTAMP_NTZ);
NULL

> SELECT cast('1900' AS TIMESTAMP_NTZ);
1900-01-01 00:00:00

> SELECT cast('1900-10-01 12:13:14' AS TIMESTAMP_NTZ);
1900-10-01 12:13:14

> SELECT cast('1900-02-30 12:13:14' AS TIMESTAMP_NTZ);
Error

> SELECT cast(DATE'1900-10-01' AS TIMESTAMP_NTZ);
1900-10-01 00:00:00

> SELECT current_timezone(), CAST(TIMESTAMP'2021-7-1T8:43:28' as TIMESTAMP_NTZ);
America/Los_Angeles 2021-07-01 08:43:28

> SELECT current_timezone(), CAST(TIMESTAMP'2021-7-1T8:43:28UTC+3' as TIMESTAMP_NTZ);
America/Los_Angeles 2021-06-30 22:43:28

> SELECT cast(DATE'1900-10-01'::VARIANT AS TIMESTAMP_NTZ);
1900-10-01 00:00:00
year-month interval​

If the targetType is a year-month interval and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS INTERVAL YEAR);
NULL

> SELECT cast('1-4' AS INTERVAL YEAR TO MONTH)::STRING;
INTERVAL '1-4' YEAR TO MONTH

> SELECT cast('1' AS INTERVAL YEAR TO MONTH);
error

> SELECT cast(INTERVAL '1-4' YEAR TO MONTH AS INTERVAL MONTH)::STRING;
INTERVAL '16' MONTH

> SELECT cast(14 AS INTERVAL YEAR TO MONTH)::STRING;
INTERVAL '1-2' YEAR TO MONTH

> SELECT cast(INTERVAL '1-11' YEAR TO MONTH AS INTERVAL YEAR)::STRING;
INTERVAL '1' YEAR
day-time interval​

If the targetType is a day-time interval and sourceExpr is of type:

SQL

> SELECT cast(NULL AS INTERVAL HOUR);
NULL

> SELECT cast('1 4:23' AS INTERVAL DAY TO MINUTE)::STRING;
INTERVAL '1 04:23' DAY TO MINUTE

> SELECT cast('1' AS INTERVAL DAY TO MINUTE);
error

> SELECT cast(INTERVAL '1 4:23' DAY TO MINUTE AS INTERVAL MINUTE)::STRING;
INTERVAL '1703' MINUTE

> SELECT cast(INTERVAL '1 4:23' DAY TO MINUTE AS INTERVAL HOUR)::STRING;
INTERVAL '28' HOUR

> SELECT cast(125.3 AS INTERVAL MINUTE TO SECOND)::STRING;
INTERVAL '2:5.3' MINUTE TO SECOND
BOOLEAN​

If the targetType is a BOOLEAN and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS BOOLEAN);
NULL

> SELECT cast('T' AS BOOLEAN);
true

> SELECT cast('True' AS BOOLEAN);
true

> SELECT cast('1' AS BOOLEAN);
true

> SELECT cast('0' AS BOOLEAN);
false

> SELECT cast('n' AS BOOLEAN);
false

> SELECT cast('on' AS BOOLEAN);
error: invalid input syntax for type boolean

> SELECT cast(0 AS BOOLEAN);
false

> SELECT cast(0.0E10 AS BOOLEAN);
false

> SELECT cast(1 AS BOOLEAN);
true

> SELECT cast(0.1 AS BOOLEAN);
true

> SELECT cast('NaN'::FLOAT AS BOOLEAN);
true

> SELECT cast(1::VARIANT AS BOOLEAN);
true
BINARY​

If the targetType is a BINARY and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS BINARY);
NULL

> SELECT hex(cast('Spark SQL' AS BINARY));
537061726B2053514C

> SELECT hex(cast('Oдesa' AS BINARY));
4FD0B4657361

> SELECT hex(cast('Oдesa'::VARIANT AS BINARY));
4FD0B4657361
ARRAY​

If the targetType is an ARRAY < targetElementType > and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS ARRAY<INT>);
NULL

> SELECT cast(array('t', 'f', NULL) AS ARRAY<BOOLEAN>);
[true, false, NULL]

> SELECT cast(array('t', 'f', NULL) AS INTERVAL YEAR);
error: cannot cast array<string> to interval year

> SELECT cast(array('t', 'f', 'o') AS ARRAY<BOOLEAN>);
error: invalid input syntax for type boolean: o.

> SELECT cast(array('t', 'f', NULL)::VARIANT AS ARRAY<BOOLEAN>);
[true, false, NULL]
MAP​

If the targetType is an MAP < targetKeyType, targetValueType > and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS MAP<STRING, INT>);
NULL

> SELECT cast(map('10', 't', '15', 'f', '20', NULL) AS MAP<INT, BOOLEAN>);
{10 -> true, 15 -> false, 20 -> null}

> SELECT cast(map('10', 't', '15', 'f', '20', NULL) AS MAP<INT, ARRAY<INT>>);
error: cannot cast map<string,string> to map<int,array<int>>

> SELECT cast(map('10', 't', '15', 'f', '20', 'o') AS MAP<INT, BOOLEAN>);
error: invalid input syntax for type boolean: o.


> SELECT schema_of_variant(parse_json('{"cars": 12, "bicycles": 5 }'));
OBJECT<bicycles: BIGINT, cars: BIGINT>
> SELECT CAST(parse_json('{"cars": 12, "bicycles": 5 }') AS MAP<STRING, INTEGER>);
{bicycles -> 5, cars -> 12}
STRUCT​

If the targetType is a STRUCT <[targetFieldName : targetFieldType [NOT NULL] [COMMENT str] [, …]] > and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS STRUCT<a:INT>);
NULL

> SELECT cast(named_struct('a', 't', 'b', '1900-01-01') AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
{"b":true,"c":1900-01-01}

> SELECT cast(named_struct('a', 't', 'b', NULL::DATE) AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
error: cannot cast struct<a:string,b:date> to struct<b:boolean,c:date>

> SELECT cast(named_struct('a', 't', 'b', '1900') AS STRUCT<b:BOOLEAN, c:ARRAY<INT>>);
error: cannot cast struct<a:string,b:string> to struct<b:boolean,c:array<int>>

> SELECT cast(named_struct('a', 't', 'b', 'hello') AS STRUCT<b:BOOLEAN, c:DATE>);
error: Cannot cast hello to DateType

> SELECT cast(named_struct('a', 't', 'b', '1900-01-01')::VARIANT AS STRUCT<b:BOOLEAN, c:DATE NOT NULL COMMENT 'Hello'>);
{"b":true,"c":1900-01-01}


> SELECT schema_of_variant(parse_json('{"name": "jason", "age": 25 }'));
OBJECT<age: BIGINT, name: STRING>
> SELECT CAST(parse_json('{"name": "jason", "age": 25 }') AS STRUCT<id: BIGINT, name: STRING>);
{"id":null,"name":"jason"}
VARIANT​

If the targetType is a VARIANT and sourceExpr is of type:

Examples​

SQL

> SELECT cast(NULL AS VARIANT);
NULL

> SELECT cast(5.1000 AS VARIANT);
5.1

> SELECT schema_of_variant(cast(5 AS VARIANT));
BIGINT

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