PostgreSQL CAST: Convert a value of One Type to Another
Summary: in this tutorial, you will learn how to use PostgreSQL CAST()
function and operator to convert a value of one type to another.
Introduction to PostgreSQL CAST() function and cast operator (::)
There are many cases in which you want to convert a value of one type into another. PostgreSQL offers the CAST()
function and cast operator (::
) to do this.
PostgreSQL CAST() function
Here’s the basic syntax of the type CAST()
function:
In this syntax:
- First, provide a
value
that you want to convert. It can be a constant, a table column, or an expression. - Then, specify the target data type to which you want to convert the
value
.
The CAST()
returns a value after it has been cast to the specified target data type. If the CAST()
function cannot cast the value to a target type, it’ll raise an error. The error message will depend on the nature of the conversion failure.
PostgreSQL cast operator (::)
Besides the type CAST()
function, you can use the following cast operator (::
) to convert a value of one type into another:
In this syntax:
value
is a value that you want to convert.target_type
specifies the target type that you want to cast the value to.
The cast operator ::
returns a value after casting the value
to the target_type
or raise an error if the cast fails.
Notice that the cast operator (::) is PostgreSQL-specific and does not conform to the SQL standard
PostgreSQL CAST() function and cast operator (::) examples
Let’s take some examples of using the CAST
operator to convert a value of one type to another.
1) Cast a string to an integer example
The following statement uses the CAST()
operator to convert a string to an integer:
Output:
If the expression cannot be converted to the target type, PostgreSQL will raise an error. For example:
2) Cast a string to a date example
This example uses the CAST()
function to convert a string to a date:
Output:
In this example, we converted 2015-01-01
literal string into January 1st 2015
and 01-OCT-2015
to October 1st 2015
.
3) Cast a string to a double example
The following example uses the CAST() function to convert a string '10.2'
into a double:
Whoops, we got the following error message:
To fix this, you need to use DOUBLE PRECISION
instead of DOUBLE
as follows:
Output:
4) Cast a string to a boolean example
This example uses the CAST()
to convert the string ‘true’, ‘T’ to true and ‘false’, ‘F’ to false:
Output:
5) Cast a string to a timestamp example
This example uses the cast operator (::) to convert a string to a timestamp:
Output:
6) Cast a string to an interval example
This example uses the cast operator to convert a string to an interval:
Output:
7) Cast a timestamp to a date example
The following example uses the CAST()
to convert a timestamp to a date:
Output:
8) Cast an interval to text
The following example uses CAST()
function to convert an interval to text:
Output:
10) Cast a JSON to a JSONB
The following example uses the CAST()
function to convert JSON to JSONB:
Output:
11) Cast a double precision to an integer
The following example uses CAST()
function to convert double precision to integer:
Output:
12) Cast an array to a text
The following example uses CAST()
function to convert an array to text:
Output:
13) Cast text to an array
The following example shows how to use the cast operator (::) to convert text to an array:
Output:
14) Using CAST with table data example
First, create a ratings
table that consists of two columns: id
and rating
:
Second, insert some sample data into the ratings
table.
Because the requirements change, we have to use the same ratings
table to store ratings as numbers 1, 2, and 3 instead of A, B, and C:
Consequentially, the ratings
table stores both alphabets & numbers.
Output:
Now, we have to convert all values in the rating
column into integers, all other A, B, C ratings will be displayed as zero.
To achieve this, you can use the CASE
expression with the type CAST
as shown in the following query:
Output:
In this example:
rating ~ E'^\\d+$'
: This expression matches the values in the rating column with a regular expressionE'^\\d+$'
. The pattern checks if a value contains only digits (\d+
) from the beginning (^
) to the end ($
). The letterE
before the string indicates is an escape string.- If the value contains only digits, the
CAST()
function converts it to an integer. Otherwise, it returns zero.
In this tutorial, you have learned how to use PostgreSQL CAST
to convert a value of one type to another.