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
| Parameter | Description |
|---|---|
| value | Required. The value to convert. |
| datatype | Required. The datatype to convert to. |
| charset | Required. 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);
4. mysql> SELECT 1 FROM
5. -> (SELECT CONVERT(t2.a USING UTF8) FROM t1, t1 t2 LIMIT 1) AS s LIMIT 1;
6. +------+
7. | 1 |
8. +------+
9. | 1 |
10. +------+
11. 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.
