Postgres – What is CAST in PostgreSQL ? | How does CAST work in PostgreSQL? How to use CAST in SQL?



Postgres – What is CAST in PostgreSQL ? | How does CAST work in PostgreSQL? How to use CAST in SQL?

Postgres - What is CAST in PostgreSQL ? | How does CAST work in PostgreSQL? How to use CAST in SQL?

In the case of handling transactions within multiple databases, data conversion is the basic requirement that is supported by almost all programming paradigms. PostgreSQL provides us with the CAST operator, which we can use to convert one data type to another data type. We can have various cast operations in the PostgreSQL like conversion of string to integers, conversion of string to date and date to a string also casting to Boolean, etc.

Syntax:

CAST (exp AS target_type );

Explanation:

target_type: Define the target data type in which we are converting the value of the exp.
How does the CAST Operator work in PostgreSQL?
The cast operator is used to convert one data type to another, where the table column or an expression’s data type is decided to be. The target data type is the data type to which the expression will get converted. The syntax of the CAST operator’s another version is as follows as well:

Syntax:

Expression::type

Consider the following example to understand the working of the PostgreSQL CAST:

Code:

SELECT
‘222’::INTEGER,
’13-MAR-2020′::DATE;

#Cast, #CastinPostgreSQL, #CastinSQL