SQL CAST vs CONVERT: Safe Type Changes Across Databases
By Sharon Ben-Moshe · August 21, 2026 · 2 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
CAST turns one SQL data type into another. Learn when CONVERT differs, how invalid values fail, and how to write portable conversions.
Quick answer
CAST is the portable SQL syntax for changing a value’s data type: `CAST(value AS type)`. CONVERT is dialect-specific and has different argument orders and capabilities. A conversion can fail when the source value is invalid, so validate or use a safe-conversion feature where the dialect provides one.
FAQs
Is CAST more portable than CONVERT?
Yes. `CAST(value AS type)` is broadly supported SQL syntax. CONVERT varies by product, including its argument order and optional formatting behavior.
What happens when SQL CAST cannot convert a value?
The behavior is dialect-specific, but an invalid conversion often raises an error. Check the database documentation and use a safe-conversion feature only when returning NULL is appropriate.
Should I CAST a column in a WHERE clause?
Avoid casting an indexed column in a predicate unless it is necessary and verified. Prefer giving the comparison value the correct type, then inspect the query plan for the target database.
Does CAST change stored data?
A CAST in a SELECT expression changes the value produced by that query, not the column’s stored definition. Changing storage type is a schema migration.
Keep reading
The Comprehensive Guide to SQL Data Types: Storage, Precision, and Cross-Dialect Portability
By Sharon Ben-Moshe · Oct 13, 2026
The complete guide to SQL data types across PostgreSQL, MySQL, SQL Server, and SQLite. Numeric precision, character sets, timestamps, and JSON.
Read postThe Definitive Guide to SQL Window Functions: Syntax, Framing, and Real-World Recipes
By Sharon Ben-Moshe · Oct 13, 2026
The comprehensive guide to SQL window functions. Master OVER, PARTITION BY, ORDER BY, window frames (ROWS vs RANGE), and ranking recipes.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.