PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Functions / Using PostgreSQL CAST To Convert From One Data Type Into Another

Using PostgreSQL CAST To Convert From One Data Type Into Another

Summary: in this tutorial, we will show you how to use PostgreSQL CAST to convert from one data type into another.

Introduction to PostgreSQL CAST

There are many cases that you want to convert one data type into another e.g., the requirements change so that you change the data type of a column. PostgreSQL provides the syntax for converting one type into another. The following illustrates the syntax of type CAST:

1
CAST ( expression AS type );

First, you specify an expression that can be a constant, a table column, etc., that you want to convert. Then, you specify the target type which you want to convert to.

PostgreSQL CAST examples

The following statement converts a string constant into an integer:

1
2
SELECT
CAST ('100' AS INTEGER);

If the expression cannot be converted to the target type, PostgreSQL will raise an error. See the following example:

1
2
SELECT
CAST ('10C' AS INTEGER);

1
2
[Err] ERROR:  invalid input syntax for integer: "10C"
LINE 2:  CAST ('10C' AS INTEGER);

In the following examples, we convert various strings to dates data type:

1
2
3
4
5
SELECT
CAST ('2015-01-01' AS DATE);
 
SELECT
CAST ('01-OCT-2015' AS DATE);

First, we converted 2015-01-01 literal string into January 1st 2015. Second, we converted 01-OCT-2015 to October 1st 2015.

In the following example, we try to convert a string into a double value:

1
2
SELECT
CAST ('10.2' AS DOUBLE);

Whoops, we got the following error message:

1
2
[Err] ERROR:  type "double" does not exist
LINE 2:  CAST ('10.2' AS DOUBLE)

To fix this, you need to use DOUBLE PRECISION instead of DOUBLE as follows;

1
2
SELECT
CAST ('10.2' AS DOUBLE PRECISION);

PostgreSQL type cast :: specific

Besides the type CAST syntax, you can use following syntax to convert a type into another:

1
expression::type

See the following examples:

1
2
3
4
5
SELECT
'100'::INTEGER;
 
SELECT
'01-OCT-2015'::DATE;

Notice that the cast syntax with :: is PostgreSQL specific and does not conform to SQL.

PostgreSQL CAST practical examples

In this section, we show you a practical example of using type CAST.

Suppose, we have a ratings table that has two columns: id and rate. The data type of the rate column is varcharbecause the ratings table stores the ratings as A, B, and C. We can create the ratings table as follows:

1
2
3
4
CREATE TABLE ratings (
ID serial PRIMARY KEY,
rate VARCHAR (1)
);

We insert some sample data into the ratings table.

1
2
3
4
5
INSERT INTO ratings (rate)
VALUES
('A'),
('B'),
('C');

Because the requirements change, we use the same ratings table to store numerical ratings.

1
2
3
4
5
INSERT INTO ratings (rate)
VALUES
(1),
(2),
(3);

So the ratings table stores mixed values including numeric and string.

1
2
3
4
SELECT
*
FROM
ratings;

Now, we have to convert all values in the rate column into integers, all other A, B, C ratings will be displayed as zero. To do this, you use the CASE operator with the type CAST as follows:

1
2
3
4
5
6
7
8
9
10
SELECT
id,
CASE
WHEN rate~E'^\\d+$' THEN
CAST (rate AS INTEGER)
ELSE
0
END as rate
FROM
ratings;

The CASE operator checks the rate, if it matches the integer pattern, it converts the rate into an integer, otherwise it returns 0.

PostgreSQL CAST example

In this tutorial, we have shown you how to use PostgreSQL CAST to convert a data type into another.

Previous Tutorial: PostgreSQL ROW_NUMBER function
Next Tutorial: PostgreSQL MAX Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.