介绍
convert 函数用于转换变量类型。
官方文档: https://dev.mysql.com/doc/refman/8.0/en/cast-functions.html 。
MySQL:cast 函数 与 convert 功能类似。
支持的类型有 char
、signed
、unsigned
date
、datetime
、time
、decimal
等。
支持转换字符集。
示例
mysql> select convert(1123, char)
+---------------------+
| convert(1123, char) |
+---------------------+
| 1123 |
+---------------------+
mysql> select convert("1123", decimal)
+--------------------------+
| convert("1123", decimal) |
+--------------------------+
| 1123 |
+--------------------------+
mysql> select convert("1123", decimal(10,2))
+--------------------------------+
| convert("1123", decimal(10,2)) |
+--------------------------------+
| 1123.00 |
+--------------------------------+
mysql> select convert('123', signed)
+------------------------+
| convert('123', signed) |
+------------------------+
| 123 |
+------------------------+
mysql> select convert('-123', unsigned)
+---------------------------+
| convert('-123', unsigned) |
+---------------------------+
| 18446744073709551493 |
+---------------------------+
mysql> select convert('-123.45', signed)
+---------------------------+
| cast('-123.45' as signed) |
+---------------------------+
| -123 |
+---------------------------+