Convert a value from one data type to another.
Explicitly converts a value from one data type to another, failing at runtime if the conversion is not possible. Preferred over implicit conversion for clarity and to avoid engine-specific coercion behaviors. Equivalent to double-colon syntax in PostgreSQL.
Compatibility
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | Native | all | CAST(expr AS type). Also CONVERT(expr, type). Supports a limited set of target types (CHAR, SIGNED, UNSIGNED, DATE, DATETIME, DECIMAL, JSON, BINARY). |
| PostgreSQL | Native | all | CAST(expr AS type). Also :: shorthand cast operator (expr::type) which is very commonly used in practice and works anywhere an expression is valid. |
| SQL Server | Native | all | CAST(expr AS type). Also CONVERT(type, expr [, style]) which adds optional style codes for date/number formatting. |
| Oracle | Native | all | CAST standard. TO_DATE(), TO_NUMBER(), TO_CHAR() are the traditional Oracle conversion functions for common types. |
| SQLite | Native | all | CAST(expr AS type). SQLite uses flexible type affinity; CAST influences storage affinity rather than strictly enforcing types. |
Details
CAST is SQL-standard. PostgreSQL's :: shorthand is unique and extremely common in practice. SQL Server's CONVERT adds format-style codes useful for date/number display.
Standard Syntax
Version Support
Per-Database Syntax & Notes
MySQL Native syntax
CAST(expr AS type). Also CONVERT(expr, type). Supports a limited set of target types (CHAR, SIGNED, UNSIGNED, DATE, DATETIME, DECIMAL, JSON, BINARY).
PostgreSQL Native syntax
CAST(expr AS type). Also :: shorthand cast operator (expr::type) which is very commonly used in practice and works anywhere an expression is valid.
SQL Server Native syntax
CAST(expr AS type). Also CONVERT(type, expr [, style]) which adds optional style codes for date/number formatting.
Oracle Native syntax
CAST standard. TO_DATE(), TO_NUMBER(), TO_CHAR() are the traditional Oracle conversion functions for common types.
SQLite Native syntax
CAST(expr AS type). SQLite uses flexible type affinity; CAST influences storage affinity rather than strictly enforcing types.