3158
MySQLERRORNotableCharacter SetsHIGH confidence

Cannot convert string to the required character set

Production Risk

Medium — data insertion fails; can cause application errors with international text.

What this means

MySQL cannot convert a string value from its current character set to the required character set for the operation, because the string contains characters that do not exist in the target character set.

Why it happens
  1. 1Storing or comparing a string with characters outside the target character set (e.g., utf8mb3 cannot store 4-byte emoji).
  2. 2CONVERT() or collation coercions between incompatible character sets.
How to reproduce
trigger — this will error
trigger — this will error
INSERT INTO t1 (name) VALUES (CONVERT('emoji text' USING latin1));

expected output

ERROR 3158 (HY000): Cannot convert string '...' from 'utf8mb4' to 'latin1'.

Fix 1

Use utf8mb4 for all columns storing Unicode text

Use utf8mb4 for all columns storing Unicode text
ALTER TABLE t1 MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Why this works

utf8mb4 supports the full Unicode range including 4-byte characters.

Fix 2

Use CONVERT() to explicitly handle encoding

Use CONVERT() to explicitly handle encoding
SELECT CONVERT(col USING utf8mb4) FROM t1;

Why this works

Explicitly converts the string to a compatible character set.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3158 ER_CANNOT_CONVERT_STRING

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors