ClickHouse常用语法
一、字符转换
1.1、转整型
toInt8(expr)
— 结果为Int8
数据类型。
toInt16(expr)
— 结果为Int16
数据类型。
toInt32(expr)
— 结果为Int32
数据类型。
toInt64(expr)
— 结果为Int64
数据类型。
输入示例:
SELECT toInt32(32), toInt16('16'), toInt8(8.8);
输出结果:
# toInt32(32) toInt16('16') toInt8(8.8) 0 32 6 8
将输入转换为uInt(不带符号整型与上述函数类似),toUInt8()
1.2、转浮点型
toFloat32(expr)
— 结果为Int32
数据类型。
toFloat64(expr)
— 结果为Int64
数据类型。
输入示例:
SELECT toFloat32('123.987'), toFloat64(12);
输出结果:
# toFloat32('123.987') toFloat64(12) 0 123.987 12
1.3、转字符串
toString()
输入示例:
SELECT now() AS now_local,--获取当前时间 toString(now()) as string_time,--将时间转换为字符串 toString(11) AS string_num; --将数字转换为字符串
输出结果:
# now_local string_time string_num 0 2021-12-16 08:47:01 2021-12-16 08:47:01 11
cast()
也可使用cast()函数对数据类型进行转换
输入示例:
SELECT CAST(8 AS varchar(50)) as num_1, toTypeName(8) type1, toTypeName(CAST(8 AS varchar(50))) type2;
输出结果:
# num_1 type1 type2 0 8 UInt8 String
1.4、转时间
toData()
toDataTime()
输入实例:
SELECT now() AS now_local,--获取当前时间 toString(now()) as string_time,--将时间转换为字符串 toDate(string_time) as data,--转换为到日的时间 toDateTime(string_time) as datatime;--转换为到秒的时间
输出结果:
# now_local string_time data datatime 0 2021-12-16 08:57:43 2021-12-16 08:57:43 2021-12-16 2021-12-16 08:57:43