Introduction
Type casting changes a value from one type to another so the database can understand or use it correctly.
value::target_type
'123'::integer456::text
'2024-06-01'::date
SELECT '100'::integer + 50 AS result, 200::text || ' apples' AS description;
'123'::integer do?'123'::integer converts the string '123' into the integer 123.price to numeric in PostgreSQL?value::type or CAST(value AS type).price::numeric, which is valid syntax. Options B, C, and D have incorrect syntax.SELECT '2023-06-01'::date + 5;
'2023-06-01'::date converts the text to a date value representing June 1, 2023.SELECT 'abc'::integer;
orders with a text column order_date storing dates as strings like '06/01/2023'. You want to find all orders after June 1, 2023. Which query correctly uses type casting with :: to achieve this?order_date is text, so to compare dates properly, it must be cast to date type.order_date to date and compares it to a date literal, which is correct. SELECT * FROM orders WHERE order_date > '2023-06-01'::date; compares text to date, which is invalid. SELECT * FROM orders WHERE order_date::text > '2023-06-01'::text; compares text to text, which is lexicographical, not date. SELECT * FROM orders WHERE order_date::integer > 20230601; tries to cast text to integer, which will cause error.Pass the test and this pattern moves unknown