转换为字符串(输出)
Boolean
如果为真值,返回1。否则返回0
mysql> select cast(true as string) , cast(false as string);
+----------------------+-----------------------+
| cast(true as string) | cast(false as string) |
+----------------------+-----------------------+
| 1 | 0 |
+----------------------+-----------------------+
Integer
按数值的十进制格式进行转换,不加前缀0,非负数不加前缀'+'号,负数加前缀'-'号。
示例:
select cast(cast("123" as int) as string) as str_value;
+-----------+
| str_value |
+-----------+
| 123 |
+-----------+
select cast(cast("-2147483648" as int) as string) as str_value;
+-------------+
| str_value |
+-------------+
| -2147483648 |
+-------------+
Float
将 float 值转换为字符串的详细规则:
-
特殊值处理:
-
NaN(非数字)转换为字符串 "NaN" -
Infinity转换为字符串 "Infinity" -
-Infinity转换为字符串 "-Infinity"
-
-
符号处理:
-
负数添加 '-' 前缀
-
正数不添加符号前缀
-
零值特殊处理:
-
-0.0转换为 "-0" -
+0.0转换为 "0"
-
-
-
格式规则:
-
使用C printf 'g' 格式说明符语义(参考https://en.cppreference.com/w/c/io/fprintf),将浮点数转换为十进制或科学记数法,具体取决于数值和有效数字位数。有效数字位数设置为7, 如果采用'e'风格转换结果的指数为X,则:
-
如果7 > X >= -4,则结果使用十进制表示法
-
否则,使用科学计数法,小数点后最多保留6位有效数字
-
删除小数点后的后缀零
-
如果小数点后没有数字,则删除小数点
-
示例:
| float | string | comment |
|---|---|---|
| 123.456 | "123.456" | |
| 1234567 | "1234567" | |
| 123456.12345 | "123456.1" | e < 7,使用科学计数法,7位有效数字 |
| 12345678.12345 | "1.234568e+07" | e >= 7,使用科学计数法,7位有效数字 |
| 0.0001234567 | "0.0001234567" | e >= -4,不使用科学计数法 |
| -0.0001234567 | "-0.0001234567" | e >= -4,不使用科学计数法 |
| 0.00001234567 | "1.234567e-05" | e < -4,使用科学计数法 |
| 123.456000 | "123.456" | Remove trailing zeros |
| 123.000 | "123" | Remove decimal point |
| 0.0 | "0" | |
| -0.0 | "-0" | Negative zero |
| NaN | "NaN" | |
| Infinity | "Infinity" | |
| -Infinity | "-Infinity" |
Double
将 double 值转换为字符串的详细规则:
-
特殊值处理:
-
NaN(非数字)转换为字符串 "NaN" -
Infinity转换为字符串 "Infinity" -
-Infinity转换为字符串 "-Infinity"
-
-
符号处理:
-
负数添加 '-' 前缀
-
正数不添加符号前缀
-
零值特殊处理:
-
-0.0转换为 "-0" -
+0.0转换为 "0"
-
-
-
格式规则:
-
使用C printf 'g' 格式说明符语义(参考https://en.cppreference.com/w/c/io/fprintf),将浮点数转换为十进制或科学记数法,具体取决于数值和有效数字位数。有效数字位数设置为16, 如果采用'e'风格转换结果的指数为X,则:
-
如果16 > X >= -4,则结果使用十进制表示法
-
否则,使用科学计数法,小数点后最多保留15位有效数字
-
删除小数点后的后缀零
-
如果小数点后没有数字,则删除小数点
-
示例:
| double | string | comment |
|---|---|---|
| 1234567890123456.12345 | "1234567890123456" | e < 16,不使用科学计数法;16位有效数字 |
| 12345678901234567.12345 | "1.234567890123457e+16" | e >= 16,使用科学计数法;16位有效数字 |
| 0.0001234567890123456789 | "0.0001234567890123457" | e >= -4,不使用科学计数法;16位有效数字 |
| 0.000000000000001234567890123456 | "1.234567890123456e-15" | e < -4,使用科学计数法;16位有效数字 |
| 123.456000 | "123.456" | Remove trailing zeros |
| 123.000 | "123" | Remove trailing decimal point |
| 0.0 | "0" | |
| -0.0 | "-0" | Negative zero |
| NaN | "NaN" | |
| Infinity | "Infinity" | |
| -Infinity | "-Infinity" |
Decimal
按数值的十进制格式进行转换,非负数不加前缀'+'号,负数加前缀'-'号,不加前缀0。
对于Decimal(P[,S])类型,在输出时,小数点后总是显示S位数字,小数位数不足S位时,后缀用0补齐。比如类型Decimal(18, 6)的数字123.456,会转换成123.456000。
示例:
select cast(cast("123.456" as decimal(18, 6)) as string) as str_value;
+------------+
| str_value |
+------------+
| 123.456000 |
+------------+
select cast(cast("-2147483648" as decimalv3(12, 2)) as string) as str_value;
+----------------+
| str_value |
+----------------+
| -2147483648.00 |
+----------------+
Date
Date 类型输出格式为 “yyyy-MM-dd”,即 4 位年,2 位月,2 位日,以 “-” 分隔。
示例如下:
select cast(date('20210304') as string);
+----------------------------------+
| cast(date('20210304') as string) |
+----------------------------------+
| 2021-03-04 |
+----------------------------------+
Datetime
Datetime 类型输出格式为 “yyyy-MM-dd HH:mm:ss[.SSSSSS]”,如果类型的 Scale 不为 0,则输出小数点及 Scale 位小数。示例如下:
select cast(cast('20210304' as datetime) as string);
+----------------------------------------------+
| cast(cast('20210304' as datetime) as string) |
+----------------------------------------------+
| 2021-03-04 00:00:00 |
+----------------------------------------------+
select cast(cast('20020304121212.123' as datetime(3)) as string);
+-----------------------------------------------------------+
| cast(cast('20020304121212.123' as datetime(3)) as string) |
+-----------------------------------------------------------+
| 2002-03-04 12:12:12.123 |
+-----------------------------------------------------------+
Time
Time 类型按照“时:分:秒”格式输出。其中小时最多 3 位,最少 2 位,且可能为负数;分钟和秒都固定为 2 位。如果类型的 Scale 不为 0,则输出小数点及 Scale 位小数。
示例如下:
select cast(cast('0' as time) as string);
+-----------------------------------+
| cast(cast('0' as time) as string) |
+-----------------------------------+
| 00:00:00 |
+-----------------------------------+
select cast(cast('2001314' as time(3)) as string);
+--------------------------------------------+
| cast(cast('2001314' as time(3)) as string) |
+--------------------------------------------+
| 200:13:14.000 |
+--------------------------------------------+
select cast(cast('-2001314.123' as time(3)) as string);
+-------------------------------------------------+
| cast(cast('-2001314.123' as time(3)) as string) |
+-------------------------------------------------+
| -200:13:14.123 |
+-------------------------------------------------+
Array
-
数组的字符串表示以左方括号
[开始,并以右方括号]结束。 -
空数组会为
[]。 -
数组元素在字符串中通过逗号加一个空格
", "进行分隔。 -
如果数组中的元素是字符串类型,其字符串表示会被单引号
'包围。 -
非字符串类型的元素会直接转换为其自身的字符串表示,不添加额外的引号。
-
数组元素为
NULL,其表示为字符串null。
mysql> select cast(array(1,2,3,4) as string);
+--------------------------------+
| cast(array(1,2,3,4) as string) |
+--------------------------------+
| [1, 2, 3, 4] |
+--------------------------------+
Map
-
Map 的字符串表示以左花括号
{开始,并以右花括号}结束。 -
如果 Map 为空,其字符串表示为
{}。 -
Map 中的键值对在字符串中通过逗号加一个空格
", "进行分隔。 -
键的表示:
-
如果键为字符串类型,则其字符串表示会被双引号
"包围。 -
如果键为
NULL,其表示为字符串null。 -
对于非字符串类型的键,直接转换为其自身的字符串表示,不添加额外的引号。
-
-
值的表示:
-
如果值为字符串类型,则其字符串表示会被双引号
"包围。 -
如果值为
NULL,其表示为字符串null。 -
对于非字符串类型的值,直接转换为其自身的字符串表示,不添加额外的引号。
-
-
键值对结构: 每个键值对的表示形式为
key:value,键和值之间用冒号:分隔。
mysql> select cast(map("abc",123,"def",456) as string);
+------------------------------------------+
| cast(map("abc",123,"def",456) as string) |
+------------------------------------------+
| {"abc":123, "def":456} |
+------------------------------------------+
Struc
-
Struct 的字符串表示以左花括号
{开始,并以右花括号}结束。 -
如果 Struct 为空,其字符串表示为
{}。 -
Struct 的字符串只会显示值,不会显示字段。
-
值的表示:
-
如果值为字符串类型,则其字符串表示会被双引号
"包围。 -
如果值为
NULL,其表示为字符串null。 -
对于非字符串类型的值,直接转换为其自身的字符串表示,不添加额外的引号。
-
-
每个值之间用逗号加一个空格
", "分隔。
mysql> select struct(123,"abc",3.14);
+-----------------------------------------+
| struct(123,"abc",3.14) |
+-----------------------------------------+
| {"col1":123, "col2":"abc", "col3":3.14} |
+-----------------------------------------+
1 row in set (0.03 sec)
mysql> select cast(struct(123,"abc",3.14) as string);
+----------------------------------------+
| cast(struct(123,"abc",3.14) as string) |
+----------------------------------------+
| {123, "abc", 3.14} |
+----------------------------------------+