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
SQL LIKE Wildcards: %, _, and ESCAPE Explained
By Sharon Ben-Moshe · Aug 21, 2026
Use SQL LIKE for text patterns without accidental matches. Learn %, _, literal wildcard escaping, case sensitivity, and portable examples.
Read postSQL String Concatenation Across Databases
By Sharon Ben-Moshe · Aug 21, 2026
Concatenate SQL strings with || or CONCAT, but NULL behavior differs by database. See portable patterns for PostgreSQL, MySQL, SQL Server, and BigQuery.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.