CONVERT
PostgreSQL equivalent:
Converts a value to the specified data type or a string to another character set.
MySQL 8.1
CONVERT(value, type | value USING charset)Parameters
- value
- Value to convert
- typekeyword
- Target data type, the same types as in CAST
- Values:BINARYCHARDATEDATETIMEDECIMALDOUBLEFLOATJSONNCHARREALSIGNEDTIMEUNSIGNEDYEAR
- USING charsetkeyword
- Character set to convert the string to, for example utf8mb4
Return value
A value of the given type or a string in the given character set; a failed conversion gives 0 or NULL with a warning
Examples
String to an integer
MySQL 8.1
SELECT CONVERT('123', SIGNED) + 1 AS number;CONVERT and CAST give the same
MySQL 8.1
SELECT CONVERT(12005.6, DECIMAL(10, 1)) AS with_convert,
CAST(12005.6 AS DECIMAL(10, 1)) AS with_cast;Binary string to text
MySQL 8.1
SELECT LOWER(CHAR(72, 105)) AS binary_string,
LOWER(CONVERT(CHAR(72, 105) USING utf8mb4)) AS text_string;Details
The CONVERT(value, type) form works like CAST(value AS type) and takes the same types. The form with USING changes the character set of a string and turns a binary string into regular text that LOWER, UPPER and case-insensitive comparison work with. In PostgreSQL the CONVERT function means something else; use CAST instead.