SQL :: cast operator
x::int is PostgreSQL shorthand for CAST(x AS int). It is the shortest cast in SQL, and no other engine here parses it.
- Categoria
- Conversion
- Returns
the target type- In this category
- 8 of 39
Sintaxe
expression::type
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | no ::; use CAST or CONVERT |
| PostgreSQL | Sim | Supported under this name. |
| MySQL | Not built in | no ::; use CAST or CONVERT |
| SQLite | Not built in | no ::; use CAST |
| Oracle | Not built in | no ::; use CAST |
Exemplos
PostgreSQL
SELECT created_at::date AS day FROM orders;
PostgreSQL
SELECT '3.14'::numeric(10, 2) AS price;
PostgreSQL
SELECT id::text || '-' || label FROM items;
Bom saber
- The shorthand is PostgreSQL-only: MySQL, SQL Server, SQLite and Oracle reject the syntax at parse time, so SQL that must travel between engines needs CAST.
- :: binds tighter than arithmetic: a + b::numeric casts only b, while (a + b)::numeric casts the sum. With integer division the difference shows: 7 / 2::numeric is 3.5, (7 / 2)::numeric is 3.
- The type name after :: is a PostgreSQL name: nvarchar, datetime and tinyint do not exist there, so casts copied from SQL Server fail on the type, not the operator.
Questions about ::
Is :: faster than CAST?
No. Both spellings resolve to the same cast operation; :: is purely shorthand. Pick by readability and by whether the query may ever run elsewhere.
Why does '13/01/2026'::date fail?
Text-to-date casts read field order from the DateStyle setting, which defaults to MDY, so 13 is taken as the month and the cast errors with 'date/time field value out of range'. For a known non-ISO format, use TO_DATE('13/01/2026', 'DD/MM/YYYY').
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL.
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.