How to Do Type Casting

Casting operations allow us to change the data type of a given value. Not all the casts are allowed, for example, you can’t cast ABC to integer, obviuslly.

In Postgres there are two valid ways or syntax to cast:

value::type

cast(value as type) 

-- Cast decimal to integer
select 2.0::integer;
select cast(2.0 AS integer)

-- Cast text to interval
select '1 month'::interval;
select '1 day'::interval;
select '2 hour'::interval;
select '5 minutes'::interval;
select cast('1 day' as interval);

-- Cast text to timestamp
select '2018-01-01 09:00:00'::timestamp;
select cast('2018-01-01 09:00:00' as timestamp);

IN THIS PAGE