SQL CONVERT function
A type conversion function in SQL Server and MySQL, with opposite argument orders and different capabilities. On PostgreSQL and Oracle, CONVERT converts character-set encoding, not types.
- Category
- Conversion
- Returns
the target type- In this category
- 2 of 39
Syntax
CONVERT(type, expression [, style]) -- SQL Server
CONVERT(expression, type) -- MySQL
Each block is a separate form. The support table and the examples show which engine takes which.
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | Supported under this name. |
| PostgreSQL | Not built in | CONVERT changes encoding, not types; use CAST or :: |
| MySQL | Yes | Supported under this name. |
| SQLite | Not built in | no CONVERT; use CAST |
| Oracle | Not built in | CONVERT converts between character sets; use CAST for types |
Examples
SQL Server
SELECT CONVERT(varchar(10), order_date, 120) AS day FROM orders;
MySQL
SELECT CONVERT(order_total, CHAR) AS total_text FROM orders;
SQL Server
SELECT TRY_CONVERT(DATE, '01/08/2026', 103) AS d;
Good to know
- Argument order is opposite: SQL Server CONVERT(target, value), MySQL CONVERT(value, target). A query ported between them usually fails to parse rather than converting the wrong thing.
- SQL Server's third style argument is the date-format codebook: CONVERT(varchar, d, 120) is ISO 'yyyy-mm-dd hh:mi:ss'. TRY_CONVERT adds NULL-on-failure.
- PostgreSQL's and Oracle's CONVERT are character-set functions: CONVERT(bytes, 'UTF8', 'LATIN1') on PostgreSQL takes and returns bytea, and CONVERT(text, 'US7ASCII') on Oracle re-encodes a string. Type conversion there is CAST (or TO_CHAR, TO_NUMBER, TO_DATE).
- MySQL's CONVERT has a second form for character sets too: CONVERT(s USING utf8mb4).
Questions about CONVERT
Is CONVERT the same in SQL Server and MySQL?
No. Argument order is reversed and the feature sets differ. SQL Server adds style codes for dates; MySQL's is closer to CAST. Use CAST for anything portable.
What are SQL Server's CONVERT style codes?
Numbers that pick a date and time format: 101 US mm/dd/yyyy, 103 British dd/mm/yyyy, 112 yyyymmdd, 120 ODBC canonical yyyy-mm-dd hh:mi:ss, 126 ISO 8601 with a T. They only exist in SQL Server.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Cast syntax in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Strip diacritics and accents in SQL Snippet
- Format a date as text in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Compute MD5 or SHA-256 hashes of values in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, MySQL.
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.