MySQL CONVERT() Function
MySQL CONVERT() Function
The MySQL CONVERT() function converts a value into a specified SQL data type or translates a string from one character set into another. It serves both as an alternative to CAST() and as a character-encoding tool.
Syntax
-- Form 1: Data type conversion
CONVERT(expr, type)
-- Form 2: Character set transcoding
CONVERT(expr USING transcoding_name)
expr: The column or expression to convert.type: Target type (CHAR,SIGNED,UNSIGNED,DECIMAL(M, D),DATE,DATETIME,TIME,BINARY,JSON).transcoding_name: A valid MySQL character set name such asutf8mb4,utf8mb3, orlatin1.
Note the comma in CONVERT(expr, type) versus the AS keyword in CAST(expr AS type).
Practical Example
Suppose demo_orders stores an external invoice_total_str text field and legacy notes. You can convert the string into a two-decimal number and normalize text encoding to utf8mb4.
SELECT
order_id,
CONVERT(invoice_total_str, DECIMAL(10, 2)) AS numeric_total,
CONVERT(customer_note USING utf8mb4) AS utf8_note
FROM demo_orders
ORDER BY order_id;
| order_id | numeric_total | utf8_note |
|---|---|---|
| 1001 | 249.95 | Deliver before 5 PM |
| 1002 | 89.00 | Leave at reception |
| 1003 | NULL | NULL |
Common Mistakes & NULL Behavior
- NULL handling: Both
CONVERT(NULL, type)andCONVERT(NULL USING charset)returnNULLsafely. - Unconvertible characters with
USING: Converting an emoji ('๐') withCONVERT(col USING latin1)replaces the character with'?'becauselatin1cannot store 4-byte UTF-8 glyphs. - Invalid type names: Writing
CONVERT('42', INT)raises a syntax error. UseSIGNEDorUNSIGNEDfor integer conversions. - Order of arguments vs SQL Server: In T-SQL (SQL Server),
CONVERT()puts the target type first (CONVERT(INT, expr)). In MySQL, the expression always comes first (CONVERT(expr, SIGNED)).
Frequently Asked Questions
When should I use CONVERT() instead of CAST() in MySQL?
Use CAST(expr AS type) for portable data type casts, and use CONVERT(expr USING charset) whenever you need to transcode text between character sets.
Can CONVERT() fix double-encoded UTF-8 data?
Yes, in migration scripts you can cast through BINARY (CONVERT(CAST(CONVERT(col USING latin1) AS BINARY) USING utf8mb4)), though you should always test on a backup copy first.
What does CONVERT('2026-13-01', DATE) return?
An invalid month or day returns NULL with a warning (or an error in strict mode depending on the statement context).
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.