Function Reference

CONVERT

PostgreSQL equivalent:

Converts a value to the specified data type or a string to another character set.

MySQL 8.4
CONVERT(value {, type | USING charset})
  • { a | b }one of the options
value
Value to convert
type
Target data type, the same types as in CAST
Keyword
BINARYCHARDATEDATETIMEDECIMALDOUBLEFLOATJSONNCHARREALSIGNEDTIMEUNSIGNEDYEAR
USING charset
Convert the string to another character set
charset
Character set, for example utf8mb4
Keyword
armscii8asciibig5binarycp1250cp1251cp1256cp1257cp850cp852cp866cp932dec8eucjpmseuckrgb18030gb2312gbkgeostd8greekhebrewhp8keybcs2koi8rkoi8ulatin1latin2latin5latin7maccemacromansjisswe7tis620ucs2ujisutf16utf16leutf32utf8mb3utf8mb4

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.4
SELECT CONVERT('123', SIGNED) + 1 AS number;
number
124

CONVERT and CAST give the same

MySQL 8.4
SELECT CONVERT(12005.6, DECIMAL(10, 1)) AS with_convert,
	CAST(12005.6 AS DECIMAL(10, 1)) AS with_cast;
with_convertwith_cast
12005.612005.6

Binary string to text

MySQL 8.4
SELECT LOWER(CHAR(72, 105)) AS binary_string,
	LOWER(CONVERT(CHAR(72, 105) USING utf8mb4)) AS text_string;
binary_stringtext_string
Hihi

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.

See also

Where to learn