SQL Server
SELECT CAST('42' AS INT) AS Ansi, CONVERT(INT, '42') AS ConvertSyntax;
CAST is ANSI and has no extras. CONVERT is SQL Server-only and takes a style argument for dates. TRY_CONVERT exists for safe casts.
CAST runs on all five engines; the native forms add format masks, style codes or a shorter spelling.
Filed under Types & casting
Write CAST when portability matters, but learn the native shorthand: :: in PostgreSQL, CONVERT for styled conversions in SQL Server, and the TO_ family with masks in Oracle.
ANSI CAST is the portable baseline: all five engines accept it. Beyond that, each engine has its own idiom, and picking the engine-native form usually buys you format masks or a shorter syntax. The examples below convert the same text to a number and back.
SELECT CAST('42' AS INT) AS Ansi, CONVERT(INT, '42') AS ConvertSyntax;
CAST is ANSI and has no extras. CONVERT is SQL Server-only and takes a style argument for dates. TRY_CONVERT exists for safe casts.
SELECT CAST('42' AS INTEGER), '42'::INTEGER, 42::TEXT;
The :: shorthand is PostgreSQL-specific and shorter than CAST; it binds tightly, so parenthesize around operators.
SELECT CAST('42' AS SIGNED), CONVERT('42', SIGNED), CAST(42 AS CHAR);
CAST and CONVERT accept a fixed list of targets (SIGNED, UNSIGNED, DECIMAL, DOUBLE, CHAR, DATE, DATETIME, JSON and a few more), not column types such as INT or VARCHAR(20).
SELECT CAST('42' AS INTEGER), CAST(42 AS TEXT);
CAST is the only explicit conversion; everything else happens silently through type affinity.
SELECT CAST('42' AS NUMBER), TO_NUMBER('42'), TO_CHAR(42) FROM dual;
CAST exists, but the TO_NUMBER/TO_CHAR/TO_DATE family is what you reach for, because it takes format masks.
For simple conversions yes: CAST(expr AS type) parses in all five. The target type names still differ per engine (INT vs INTEGER vs NUMBER), so portable SQL uses each engine's spelling.
:: predates full ANSI support and stuck because it is short and plays well in generated SQL. CAST exists alongside it and both compile to the same operation.
No. MySQL CAST only converts to fixed types. For masks you use FORMAT(), DATE_FORMAT(), LPAD() and similar functions instead.
Text '42' becomes 42 everywhere, but 'abc' raises an error in three engines and quietly becomes 0 in two.
Five engines, four different formatting idioms for the same 1,234,567.89.
A plain CAST of 'abc' errors in SQL Server, PostgreSQL and Oracle and quietly returns 0 in MySQL and SQLite.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.