Type Conversion Functions

Syntax

CAST(value AS type)

Syntax Description

This function is used to forcibly convert types.

Precautions

Example

Convert amount into a string. The specified length of the string is invalid after the conversion.

insert into temp select cast(amount as VARCHAR(10)) from source_stream;

Common Type Conversion Functions

Table 1 Common type conversion functions

Function

Description

cast(v1 as varchar)

Converts v1 to a string. The value of v1 can be of the numeric type or of the timestamp, date, or time type.

cast (v1 as int)

Converts v1 to the int type. The value of v1 can be a number or a character.

cast(v1 as timestamp)

Converts v1 to the timestamp type. The value of v1 can be of the string, date, or time type.

cast(v1 as date)

Converts v1 to the date type. The value of v1 can be of the string or timestamp type.

Detailed Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** source **/
CREATE
SOURCE STREAM car_infos (cast_int_to_varchar int, cast_String_to_int string,
case_string_to_timestamp string, case_timestamp_to_date timestamp) WITH (
  type = "dis",
  region = "xxxxx",
  channel = "dis-input",
  partition_count = "1",
  encode = "json",
  offset = "13",
  json_config =
"cast_int_to_varchar=cast_int_to_varchar;cast_String_to_int=cast_String_to_int;case_string_to_timestamp=case_string_to_timestamp;case_timestamp_to_date=case_timestamp_to_date"
 
);
/** sink **/
CREATE
SINK STREAM cars_infos_out (cast_int_to_varchar varchar, cast_String_to_int
int, case_string_to_timestamp timestamp, case_timestamp_to_date date) WITH (
  type = "dis",
  region = "xxxxx",
  channel = "dis-output",
  partition_count = "1",
  encode = "json",
  offset = "4",
  json_config =
"cast_int_to_varchar=cast_int_to_varchar;cast_String_to_int=cast_String_to_int;case_string_to_timestamp=case_string_to_timestamp;case_timestamp_to_date=case_timestamp_to_date",
  enable_output_null="true"
);
/** Statistics on static car information**/
INSERT
INTO
  cars_infos_out
SELECT
  cast(cast_int_to_varchar as varchar),
  cast(cast_String_to_int as int),
  cast(case_string_to_timestamp as timestamp),
  cast(case_timestamp_to_date as date)
FROM
  car_infos;

Returned data

{"case_string_to_timestamp":1514736001000,"cast_int_to_varchar":"5","case_timestamp_to_date":"2018-01-01","cast_String_to_int":100}