CAST(value AS type)
This function is used to forcibly convert types.
The following example converts the amount value to an integer.
insert into temp select cast(amount as INT) from source_stream;
Example |
Description |
Example |
---|---|---|
cast(v1 as string) |
Converts v1 to a string. The value of v1 can be of the numeric type or of the timestamp, date, or time type. |
Table T1: | content (INT) | | ------------- | | 5 | Statement: SELECT cast(content as varchar) FROM T1; Result: "5" |
cast (v1 as int) |
Converts v1 to the int type. The value of v1 can be a number or a character. |
Table T1: | content (STRING) | | ------------- | | "5" | Statement: SELECT cast(content as int) FROM T1; Result: 5 |
cast(v1 as timestamp) |
Converts v1 to the timestamp type. The value of v1 can be of the string, date, or time type. |
Table T1: | content (STRING) | | ------------- | | "2018-01-01 00:00:01" | Statement: SELECT cast(content as timestamp) FROM T1; Result: 1514736001000 |
cast(v1 as date) |
Converts v1 to the date type. The value of v1 can be of the string or timestamp type. |
Table T1: | content (TIMESTAMP) | | ------------- | | 1514736001000 | Statement: SELECT cast(content as date) FROM T1; Result: "2018-01-01" |
Flink jobs do not support the conversion of bigint to timestamp using CAST. You can convert it using to_timestamp.
CREATE TABLE kafkaSource ( cast_int_to_string int, cast_String_to_int string, case_string_to_timestamp string, case_timestamp_to_date timestamp ) WITH ( 'connector' = 'kafka', 'topic' = 'KafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', "format" = "json" ); CREATE TABLE printSink ( cast_int_to_string string, cast_String_to_int int, case_string_to_timestamp timestamp, case_timestamp_to_date date ) WITH ( 'connector' = 'print' ); insert into printSink select cast(cast_int_to_string as string), cast(cast_String_to_int as int), cast(case_string_to_timestamp as timestamp), cast(case_timestamp_to_date as date) from kafkaSource;
{"cast_int_to_string":"1", "cast_String_to_int": "1", "case_string_to_timestamp": "2022-04-02 15:00:00", "case_timestamp_to_date": "2022-04-02 15:00:00"}
+I(1,1,2022-04-02T15:00,2022-04-02)