// conversion reference

SQL CONVERT ... USING (MySQL)

Converts a string between character sets: CONVERT('café' USING utf8mb4) re-encodes the text. One USING means charset; two arguments mean type.

Categoria
Conversion
Returns
string
In this category
7 of 39

Sintaxe

CONVERT(string USING charset)
CONVERT(value, type)

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Not built in No built-in equivalent.
PostgreSQL Not built in No built-in equivalent.
MySQL Sim Supported under this name.
SQLite Not built in No built-in equivalent.
Oracle Not built in No built-in equivalent.

Exemplos

MySQL

SELECT CONVERT('café' USING utf8mb4) AS c;

MySQL

SELECT CONVERT(42, CHAR) AS num, CHARSET(CONVERT('x' USING utf8mb4)) AS cs;
-- 42, utf8mb4

Bom saber

  • USING converts character sets, not types: CONVERT(42, CHAR) changes type, while CONVERT(42 USING utf8mb4) turns the number into a utf8mb4 string as a side effect. The shapes look similar and do different jobs.
  • Unrepresentable characters become '?': CONVERT('é' USING ascii) is '?', with only a warning. Verify with HEX before and after converting real data.
  • The source charset is the string's own: inspect it with CHARSET(s) first; converting from binary reinterprets the bytes.
  • It is MySQL-only: PostgreSQL converts with convert_to/convert_from, SQL Server with collations and nvarchar, Oracle with CONVERT(s, dest_charset, source_charset).

Questions about CONVERT ... USING

Does CONVERT(x USING cs) change the data type?

No, only the character set. CONVERT(x, type) with two arguments is the type conversion; USING is always charset transcoding.

Why did my converted text lose characters?

The target charset cannot represent them, so MySQL substitutes '?' and raises a warning. SHOW WARNINGS after the conversion, and compare HEX output before and after.

How do I check a string's current charset?

CHARSET(s) and COLLATION(s) in MySQL report the value's charset and collation, the first step before any USING conversion.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: MySQL.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.