Type Casts
Anonymous contributor
Published May 29, 2024
Contribute to Docs
In PostgreSQL, Type Casts converts a value from one data type to another specified data type. This can be done explicitly using the CAST()
function and the ::
operator.
Syntax
CAST()
function:
CAST(value AS target_data_type)
::
operator:
value::target_data_type
Examples
Example 1
Casting a floating-point number to an integer using the CAST()
function:
SELECT CAST(37.8 AS INTEGER);
Output:
int4 |
---|
38 |
Example 2
Casting a string to a date using the CAST()
function:
SELECT CAST('Wed 22 May 2024 18:33:55' AS DATE);
Output:
date |
---|
2024-05-22 |
Example 3
Casting a string to a floating-point number using the ::
operator:
SELECT '22.5'::FLOAT;
Output:
float8 |
---|
22.5 |
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PostgreSQL on Codecademy
- Skill path
Design Databases With PostgreSQL
Learn how to query SQL databases and design relational databases to efficiently store large quantities of data.Includes 5 CoursesWith CertificateBeginner Friendly13 hours - Free course
Getting Started Off-Platform for Data Science
Learn how to setup Jupyter Notebooks and PostGRESQL and run data science projects on your own computer locally!Beginner Friendly1 hour