CONVERT

Description

The CONVERT() function converts a value into the specified datatype or character set.

Syntax

  1. > CONVERT(value, type)

Or:

  1. > CONVERT(value USING charset)

Parameter Values

ParameterDescription
valueRequired. The value to convert.
datatypeRequired. The datatype to convert to.
charsetRequired. The character set to convert to.

Currently, convert can support following conversion:

  • Conversion between numeric types, mainly including SIGNED, UNSIGNED, FLOAT, and DOUBLE type.
  • Numeric types to character CHAR type.
  • Numeric character types to numerical types(negative into SIGNED).

Examples

  1. mysql> select convert(150,char);
  2. +-------------------+
  3. | cast(150 as char) |
  4. +-------------------+
  5. | 150 |
  6. +-------------------+
  7. 1 row in set (0.01 sec)
  1. CREATE TABLE t1(a tinyint);
  2. INSERT INTO t1 VALUES (127);
  3. mysql> SELECT 1 FROM
  4. -> (SELECT CONVERT(t2.a USING UTF8) FROM t1, t1 t2 LIMIT 1) AS s LIMIT 1;
  5. +------+
  6. | 1 |
  7. +------+
  8. | 1 |
  9. +------+
  10. 1 row in set (0.00 sec)

Constraints

  • Non-numeric character types cannot be converted to numeric types.
  • Numeric and character types with formats of Data cannot be converted to Date.
  • Date and Datetime types cannot be converted to character types.
  • Date and Datetime cannot be converted to each other.