Hive支持使用内置函数进行数据类型转换
CAST(column_name AS INT)
例如,将名为age
的字符串列转换为整数类型:
SELECT CAST(age AS INT) FROM table_name;
CAST(column_name AS STRING)
例如,将名为id
的整数列转换为字符串类型:
SELECT CAST(id AS STRING) FROM table_name;
CAST(column_name AS INT)
例如,将名为price
的浮点数列转换为整数类型(会截断小数部分):
SELECT CAST(price AS INT) FROM table_name;
CAST(column_name AS FLOAT)
例如,将名为quantity
的整数列转换为浮点数类型:
SELECT CAST(quantity AS FLOAT) FROM table_name;
CAST(column_name AS FLOAT)
例如,将名为price
的字符串列转换为浮点数类型:
SELECT CAST(price AS FLOAT) FROM table_name;
CAST(from_unixtime(unix_timestamp(column_name, 'yyyy-MM-dd')) AS STRING)
例如,将名为date
的日期列转换为字符串类型(格式为yyyy-MM-dd
):
SELECT CAST(from_unixtime(unix_timestamp(date, 'yyyy-MM-dd')) AS STRING) FROM table_name;
CAST(from_unixtime(unix_timestamp(column_name, 'pattern'), 'yyyy-MM-dd') AS DATE)
例如,将名为date_str
的字符串列转换为日期类型(格式为yyyy-MM-dd
):
SELECT CAST(from_unixtime(unix_timestamp(date_str, 'yyyy-MM-dd'), 'yyyy-MM-dd') AS DATE) FROM table_name;
请注意,这些示例中的column_name
和table_name
需要替换为实际的列名和表名。另外,根据您的数据类型和需求,您可能需要调整日期格式。