CAST / Type Conversion
SQL · String Functionssyntax
CAST(expression AS data_type)
expression::data_type -- PostgreSQL shorthandexample
SELECT
CAST(price AS INTEGER) AS rounded_price,
CAST(order_date AS VARCHAR) AS date_string,
'42'::INTEGER + 8 AS sum_pg -- PostgreSQL only
FROM products;output
-- rounded_price | date_string | sum_pg
-- 29 | 2025-03-15 | 50Note CAST is ANSI standard. PostgreSQL's :: shorthand is shorter but not portable. Be careful casting — CAST('abc' AS INTEGER) will throw an error. Use TRY_CAST in SQL Server for safe conversions.