Type conversion functions convert one data type to another. For example, converting between numbers, time, and strings.
Example: Converting time strings to the date and time data types
In MySQL tenants, you can directly copy time strings to MySQL that can automatically convert them into date and time data types, or use the CONVERT or CAST function to convert the data types. SQL statement:
obclient> SELECT CONVERT('2020-02-02 14:30:45', date) t1
, CONVERT('2020-02-02 14:30:45', time) t2
, CONVERT('2020-02-02 14:30:45', datetime) t3
, CAST('2020-02-02 14:30:45' AS date) t4
, CAST('2020-02-02 14:30:45' AS time) t5
, CAST('2020-02-02 14:30:45' AS datetime) t6
\G
*************************** 1. row ***************************
t1: 2020-02-02
t2: 14:30:45
t3: 2020-02-02 14:30:45
t4: 2020-02-02
t5: 14:30:45
t6: 2020-02-02 14:30:45
1 row in set (0.00 sec)
In MySQL tenants, you can use the DATE_FORMAT function to convert date and time data types to strings.
Example: Converting between numbers and strings
In MySQL tenants, you can use the CONVERT and CAST functions to convert numbers from and to strings.
obclient> SELECT convert('3.1415926', decimal) n1
, cast('3.1415926' AS decimal) n2
, convert(3.1415926, char(10)) s1
, cast(3.1414926 AS char(10)) s2
;
+------+------+-----------+-----------+
| n1 | n2 | s1 | s2 |
+------+------+-----------+-----------+
| 3 | 3 | 3.1415926 | 3.1414926 |
+------+------+-----------+-----------+
1 row in set (0.00 sec)