Skip to content

CONVERT

Function Description

The CONVERT() function converts a value to the specified data type or character set.

Grammar Structure

> CONVERT(value, type)

or:

> CONVERT(value USING charset)
Parameters Description
value Required parameters, value to be converted
datatype Required parameters, target data type
charset Required parameters, target character set

Currently, convert can be converted as follows:

  • Conversion between numeric types mainly includes SIGNED, UNSIGNED, FLOAT, DOUBLE types
  • Numeric type to character CHAR type conversion
  • Convert character types with numeric format to numeric type (negative numbers need to be converted to SIGNED)

Example

mysql> select convert(150,char);
+-------------------------+
| cast(150 as char) |
+-------------------------+
| 150 |
+-------------------------+
1 row in set (0.01 sec)
CREATE TABLE t1(a tinyint);
INSERT INTO t1 VALUES (127);

mysql> SELECT 1 FROM
  -> (SELECT CONVERT(t2.a USING UTF8) FROM t1, t1 t2 LIMIT 1) AS s LIMIT 1;
+------+
| 1 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

limit

  • Non-numeric character types cannot be converted to numeric type
  • The numerical type and character type of the date format cannot be converted to Date type
  • Date, Datetime type cannot be converted to character type
  • Date and Datetime cannot convert each other