0
0
PostgreSQLquery~5 mins

Type casting with :: operator in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the :: operator do in PostgreSQL?
It converts a value from one data type to another, also called type casting.
Click to reveal answer
beginner
How do you cast a string '123' to an integer using the :: operator?
You write '123'::integer to convert the string to an integer.
Click to reveal answer
intermediate
Can you use the :: operator to cast between any two types?
No, only between compatible types. For example, casting text to integer works if the text is a valid number.
Click to reveal answer
intermediate
What happens if you try to cast a non-numeric string to integer using ::?
PostgreSQL will return an error because the string cannot be converted to an integer.
Click to reveal answer
intermediate
Is :: operator specific to PostgreSQL or standard SQL?
The :: operator is specific to PostgreSQL and some other databases; standard SQL uses the CAST() function.
Click to reveal answer
What does '2024-06-01'::date do in PostgreSQL?
AReturns the string unchanged
BConverts the string to an integer
CCauses a syntax error
DConverts the string to a date type
Which of these is a valid use of the :: operator?
A<code>123::text</code>
B<code>'abc'::integer</code>
C<code>'true'::integer</code>
D<code>NULL::boolean</code>
What error occurs if you cast 'hello'::integer?
ANo error, returns 0
BSyntax error
CInvalid input syntax for integer error
DReturns NULL
Which is the standard SQL alternative to :: for casting?
ACAST(value AS type)
BCONVERT(value, type)
CTO_TYPE(value)
DCHANGE_TYPE(value)
What type will 123.45::integer return?
A123.45 as float
B123 as integer
CError because of decimal
DNULL
Explain how the :: operator works in PostgreSQL and give an example.
Think about converting one data type to another.
You got /3 concepts.
    What are the risks or errors you might face when using the :: operator?
    Consider what happens if the value cannot be converted.
    You got /3 concepts.