转换函数
本页按功能分类,全面概述了 TiDB Cloud Lake 中的转换函数,便于参考。
类型转换函数
数值转换函数
二进制和专用转换函数
将值从一种类型转换为另一种类型时,请注意以下事项:
当从浮点数、小数或字符串转换为整数,或转换为带小数部分的小数时,TiDB Cloud Lake 会将值四舍五入到最接近的整数。这由设置
numeric_cast_option决定(默认值为'rounding'),该设置控制数值类型转换操作的行为。当numeric_cast_option显式设置为'truncating'时,TiDB Cloud Lake 会截断小数部分,丢弃所有小数值。SELECT CAST('0.6' AS DECIMAL(10, 0)), CAST(0.6 AS DECIMAL(10, 0)), CAST(1.5 AS INT); ┌──────────────────────────────────────────────────────────────────────────────────┐ │ cast('0.6' as decimal(10, 0)) │ cast(0.6 as decimal(10, 0)) │ cast(1.5 as int32) │ ├───────────────────────────────┼─────────────────────────────┼────────────────────┤ │ 1 │ 1 │ 2 │ └──────────────────────────────────────────────────────────────────────────────────┘ SET numeric_cast_option = 'truncating'; SELECT CAST('0.6' AS DECIMAL(10, 0)), CAST(0.6 AS DECIMAL(10, 0)), CAST(1.5 AS INT); ┌──────────────────────────────────────────────────────────────────────────────────┐ │ cast('0.6' as decimal(10, 0)) │ cast(0.6 as decimal(10, 0)) │ cast(1.5 as int32) │ ├───────────────────────────────┼─────────────────────────────┼────────────────────┤ │ 0 │ 0 │ 1 │ └──────────────────────────────────────────────────────────────────────────────────┘下表汇总了数值类型转换操作,展示了不同源数值数据类型与目标数值数据类型之间的转换可能性。请注意,其中还说明了从 String 转换为 Integer 的要求,即源字符串必须包含一个整数值。
TiDB Cloud Lake 还提供了多种函数,用于将表达式转换为不同的日期和时间格式。更多信息,请参见 日期与时间函数。